|
1 | | -ptpython |
2 | | -======== |
| 1 | +ptpython: a better Python REPL |
| 2 | +============================== |
3 | 3 |
|
4 | | -`ptpython` is a Python REPL build on top of the `prompt_toolkit |
5 | | -<http://github.com/jonathanslenders/python-prompt-toolkit>`_ library. For now |
6 | | -it's still still shipped with ``prompt_toolkit`` itself. Maybe, later on (when |
7 | | -APIs are sure to be stable) we will pull it out and put it here instead. |
| 4 | +``ptpython`` is an advanced Python REPL build on top of the `prompt_toolkit |
| 5 | +<http://github.com/jonathanslenders/python-prompt-toolkit>`_ library. |
8 | 6 |
|
9 | | -To install ``ptpython``, run: |
| 7 | +It works best on all Posix systems like Linux, BSD and OS X. But it should work |
| 8 | +as well on Windows. It works on all Python versions from 2.6 up to 3.4. |
| 9 | + |
| 10 | + |
| 11 | +Installation |
| 12 | +************ |
| 13 | + |
| 14 | +To install ``ptpython``, type: |
10 | 15 |
|
11 | 16 | :: |
12 | 17 |
|
13 | | - pip install prompt-toolkit |
| 18 | + pip install ptpython |
| 19 | + |
| 20 | + |
| 21 | +The REPL |
| 22 | +******** |
| 23 | + |
| 24 | +Run ``ptpython`` to get an interactive Python prompt with syntax highlighting, |
| 25 | +code completion, etc... |
| 26 | + |
| 27 | +.. image :: docs/images/example1.png |
| 28 | +
|
| 29 | +By default, you will have Emacs key bindings, but if you prefer Vi bindings |
| 30 | +(like in the above screenshot) then run ``ptpython --vi``. |
| 31 | + |
| 32 | +If you want to embed the REPL inside your application at one point, do: |
| 33 | + |
| 34 | +.. code:: python |
| 35 | +
|
| 36 | + from ptpython.repl import embed |
| 37 | + embed(globals(), locals(), vi_mode=False, history_filename=None) |
| 38 | +
|
| 39 | +
|
| 40 | +Autocompletion |
| 41 | +************** |
| 42 | + |
| 43 | +``Tab`` and ``shift+tab`` complete the input. |
| 44 | +In Vi-mode, you can also use ``Ctrl+N`` and ``Ctrl+P``. |
| 45 | + |
| 46 | +There is even completion on file names inside strings: |
| 47 | + |
| 48 | +.. image :: docs/images/file-completion.png |
| 49 | +
|
| 50 | +
|
| 51 | +Multiline editing |
| 52 | +***************** |
| 53 | + |
| 54 | +Usually, multi-line editing mode will automatically turn on when you press enter |
| 55 | +after a colon, however you can always turn it on by pressing ``F7``. |
| 56 | + |
| 57 | +To execute the input in multi-line mode, you can either press ``Alt+Enter``, or |
| 58 | +``Esc`` followed by ``Enter``. (If you want the first to work in the OS X |
| 59 | +terminal, you have to check the "Use option as meta key" checkbox in your |
| 60 | +terminal settings. For iTerm2, you have to check "Left option acts as +Esc" in |
| 61 | +the options.) |
| 62 | + |
| 63 | +.. image :: docs/images/multiline.png |
| 64 | +
|
| 65 | +
|
| 66 | +Tab pages |
| 67 | +********* |
| 68 | + |
| 69 | +Suppose that you were editing a long multiline Python command, but suddenly you |
| 70 | +want to test something else in between in the same shell and go back. We have |
| 71 | +tab pages for this. Each tab has its own input buffer, but they are all |
| 72 | +evaluated in the same environment (sharing locals/globals). |
| 73 | + |
| 74 | +.. image :: docs/images/tab-pages.png |
| 75 | +
|
| 76 | +The shortcuts: |
| 77 | + |
| 78 | ++------------------------+-----------------------+ |
| 79 | +| Create a new tab page | Control-N | |
| 80 | ++------------------------+-----------------------+ |
| 81 | +| Close current tab page | Control-D | |
| 82 | ++------------------------+-----------------------+ |
| 83 | +| Navigate to next tab | Control-Arrow-right | |
| 84 | +| | (Or in Vi-mode: 'gt') | |
| 85 | ++------------------------+-----------------------+ |
| 86 | +| Navigate to previous | Control-Arrow-left | |
| 87 | +| tab | (Or in Vi-mode: 'gT') | |
| 88 | ++------------------------+-----------------------+ |
| 89 | + |
| 90 | +Other features |
| 91 | +*************** |
| 92 | + |
| 93 | +Running system commands: Press ``Meta-!`` in Emacs mode or just ``!`` in Vi |
| 94 | +navigation mode to see the "Shell command" prompt. There you can enter system |
| 95 | +commands without leaving the REPL. |
| 96 | + |
| 97 | +Selecting text: Press ``Control+Space`` in Emacs mode on ``V`` (major V) in Vi |
| 98 | +navigation mode. |
| 99 | + |
| 100 | + |
| 101 | +You love IPython? |
| 102 | +***************** |
| 103 | + |
| 104 | +Run ``ptipython`` (prompt_toolkit - IPython), to get a nice interactive shell |
| 105 | +with all the power that IPython has to offer, like magic functions and shell |
| 106 | +integration. Make sure that IPython has been installed. (``pip install |
| 107 | +ipython``) |
| 108 | + |
| 109 | +.. image :: docs/images/ipython.png |
| 110 | +
|
| 111 | +
|
| 112 | +You are using Django? |
| 113 | +********************* |
| 114 | + |
| 115 | +`django-extensions <https://github.com/django-extensions/django-extensions>`_ |
| 116 | +has a ``shell_plus`` management command. When ``ptpython`` has been installed, |
| 117 | +it will by default use ``ptpython`` or ``ptipython``. |
| 118 | + |
| 119 | + |
| 120 | +PDB |
| 121 | +*** |
| 122 | + |
| 123 | +There is an experimental PDB replacement: `ptpdb |
| 124 | +<https://github.com/jonathanslenders/ptpdb>`_. |
| 125 | + |
| 126 | + |
| 127 | +About Windows support |
| 128 | +********************* |
| 129 | + |
| 130 | +``prompt_toolkit`` works still a little better on systems like Linux and OS X |
| 131 | +than on Windows, but it certainly is usable. One thing that still needs |
| 132 | +attention is the colorscheme. Windows terminals don't support all colors, so we |
| 133 | +have to create another colorscheme for Windows. |
| 134 | + |
| 135 | +.. image :: docs/images/windows.png |
| 136 | +
|
| 137 | +
|
| 138 | +FAQ |
| 139 | +--- |
| 140 | + |
| 141 | +Q |
| 142 | + The ``Ctrl-S`` forward search doesn't work and freezes my terminal. |
| 143 | +A |
| 144 | + Try to run ``stty -ixon`` in your terminal to disable flow control. |
| 145 | + |
| 146 | +Q |
| 147 | + The ``Meta``-key doesn't work. |
| 148 | +A |
| 149 | + For some terminals you have to enable the Alt-key to act as meta key, but you |
| 150 | + can also type ``Escape`` before any key instead. |
| 151 | + |
| 152 | + |
| 153 | +Alternatives |
| 154 | +************ |
| 155 | + |
| 156 | +Have a look at the alternatives. |
| 157 | + |
| 158 | +- `BPython <http://bpython-interpreter.org/downloads.html>`_ |
| 159 | + |
| 160 | +If you find another alternative, you can create an issue and we'll list it |
| 161 | +here. If you find a nice feature somewhere that is missing in ``ptpython``, |
| 162 | +also create a GitHub issue and mabye we'll implement it. |
| 163 | + |
| 164 | + |
| 165 | +Special thanks to |
| 166 | +***************** |
| 167 | + |
| 168 | +- `Pygments <http://pygments.org/>`_: Syntax highlighter. |
| 169 | +- `Jedi <http://jedi.jedidjah.ch/en/latest/>`_: Autocompletion library. |
| 170 | +- `Docopt <http://docopt.org/>`_: Command-line interface description language. |
| 171 | +- `wcwidth <https://github.com/jquast/wcwidth>`_: Determine columns needed for a wide characters. |
| 172 | +- `prompt_toolkit <http://github.com/jonathanslenders/python-prompt-toolkit>`_ for the interface. |
| 173 | + |
| 174 | +.. |PyPI| image:: https://pypip.in/version/prompt-toolkit/badge.svg |
| 175 | + :target: https://pypi.python.org/pypi/prompt-toolkit/ |
| 176 | + :alt: Latest Version |
0 commit comments