Ptpython: A Better Python REPL | by Khuyen Tran | Towards Data Science

Have you ever wanted to quickly try some ideas popping up in your head using a Python Shell (REPL)? You might not want to open a new Jupyter Notebook to experiment with only a few lines of code.
But you might also be hesitant to use a classic Python shell since it doesn’t support auto-completion or docstring as Jupyter Notebook does. You also cannot fix the mistake in the code after hitting Enter.
What if you can turn your boring Python shell into a multi-functional shell like below?
That is when ptpython comes in handy.
Ptpython can be considered as a better REPL. To install ptpython, type:
pip install ptpython
Then type:
ptpython
... to start using ptpython.
If you make a mistake on a classical Python shell and hit Enter, you will not be able to go back to fix the mistake.
Luckily, ptpython allows you to validate the input when hitting the Enter button. In the GIF below, I miss a closing bracket and receive an error, but I can go back and fix my code until there is no more error!
Have you ever wished to get the suggestion based on the history? That could be done with ptpython.
By default, this feature is not enabled. But you can enable it by pressing F2 to access the menu. Then use the right or left arrow to turn on the “Auto suggestion” feature. Finally, hit Enter to hide the menu.
Now, you should see the suggestion based on history when typing. To accept the suggestion, simply press the right arrow.
You can also view all methods that belong to a specific class after typing dot (.) like below:
Use the down arrow to get to a certain suggestion, then continue typing the rest of the code.
You can also access the history by pressing F3. Use the up or down arrow to get to the line of code you want to copy, then type Space to choose the code for insertion.
Whenever you are done with selecting code, press Enter and these lines of code will be pasted in your current shell!
Note that the code will be inserted by the order of execution.
Have you ever wished to edit the code pasted on your Python shell? A classic Python shell will not allow you to do that.
Ptpython allows you to edit the pasted code until you are happy with it:
To turn on the paste mode, press F6. When the paste mode is on, the code will not be executed when you press Enter. Whenever you are ready to execute the code, press F6 again to turn off the paste mode, then press Enter twice.
Ptpython allows you to view the parameters of your Python functions or classes like below:
You can also view the docstring of a class or a function. To enable this feature, press F2 then turning on the “Show docstring” feature.
Now you can see the docstring of the function or the class you are using!
Ptpython also makes it easier to distinguish between parentheses by highlighting matching parathesis.
If you want to increase readability, you can also increase the blank line after the output.
To enable this feature, press F2 then turn on “Blank line after input” and “Blank line after output”.
Ptpython also supports beautiful syntax highlighting like below!
You can change the theme by pressing the right arrow until you find your favorite theme.
There are a total of 39 themes available. If you want to have the same theme as Sublime Text, choose the Monokai theme.
Ptpython also supports IPython. Run ptipython , to get a nice interactive shell with all the power that IPython has to offer, including the magic commands!
The changes you make to the setting of the current ptpython shell will be undone when you quit the shell.
To change the configuration permanently, copy this file to $XDG_CONFIG_HOME/ptpython/config.py . On Linux, this is: ~/.config/ptpython/config.py .
After copying the config file to your local directory, you should have all the features I show above enabled. Feel free to change the config files based on your preferences.
Congratulations! You have just learned what ptpython is and how it can be helpful. I only cover a few of my favorite features in this article so I encourage you to install ptpython and try it yourself.
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: