Ptpython: Improved REPL for Python

Have you ever wanted to quickly try out a fresh idea using the Python command line interface, the REPL? When it comes to experimenting with just a few lines of code, you probably don't want to create a new Jupyter notebook for that.



But in such a situation, you may not be particularly pleased with the prospect of using the classic Python console, since, unlike Jupyter Notebook, it does not support auto-completion and does not know how to work with document lines. In the REPL, in addition, it is impossible, after clicking on Enter



, to correct errors in the code.



What if you could turn a rather boring Python command line into a multifunctional tool like the one shown below?





Advanced Python command line



Actually, the ptpython project is focused on those who have such a desire.



What is ptpython?



Ptpython can be called an improved Python command line interface. You can install it like this:



pip install ptpython

      
      





And in order to start it, you just need to use the following command:



ptpython

      
      





Data entry capabilities



▍Checking input data



If, while working in the classic Python command line, you make a mistake when entering a command, then after clicking on Enter



, you cannot go back to the wrong code and fix it.





A mistake made on the regular Python command line



But ptpython allows you to check what is entered from the keyboard, even before clicking on Enter



. The following animation shows that a missing closing parenthesis causes an error message to appear. This error can be corrected immediately.





Bug fix when working in ptpython 



▍Auto completion based on historical data



If you've ever thought about using history-based auto-completion when working with the Python command line, know that ptpython supports it.





Autocompletion based on history data



But this feature of ptpython is not enabled by default. However, in order to enable it, it is enough, using the key F2



, to call the menu, in which, using the arrow keys, you need to find the option Auto suggestion



and transfer it to the state on



. To close the menu, click on Enter



.





Enabling autocompletion



After enabling this option Auto suggestion



, you should have history-based autocompletion work. To take advantage of what ptpython has to offer, all you have to do is press an arrow key



.



▍Using hints when entering code



If you enter a dot while working with an object, a list of its properties and methods will be displayed.





Hints displayed after entering a point



Use the arrow keys to select the desired option, and after selecting the appropriate option, you can continue entering your code.



▍Insert data from command history



You can view the command history by pressing the key F3



. In order to select the code that needs to be copied into the workspace from the history panel, you need to go to the corresponding line with the arrow keys and press the key



.



After the selection of the desired section of the code is completed, just click on Enter



and the corresponding code will be inserted into the work area.





Copying code from the history pane



Note that the code will be inserted into the Stage in the order in which it is executed.



▍ Insert Mode



Have you ever wanted to edit the code pasted into the Python command line? This cannot be done in a regular Python REPL.





Working in a regular Python command line



And ptpython allows you to edit the inserted code, bringing it to the desired state.





Editing Inserted Code in ptpython



To enable insert mode, just press the key F6



. When this mode is activated, the code, when pressed Enter



, will not be executed. And after the code is ready for execution, you need to press again F6



to turn off the insert mode, and then double-click on Enter



.



Data output capabilities



▍View function signatures and docstrings



Ptpython allows you to view information about the parameters of functions and constructors.





Viewing information about the DataFrame constructor You



can also view the documentation strings for classes and functions. To enable this feature, open the menu ( F2



), and then enable the option Show docstring



.





Enabling the Output of Documentation Strings



You can now view the documentation for the programming constructs used.





Documentation output



▍Selection of paired brackets



In order to make it easier to work with complex constructs that use parentheses, ptpython can match parentheses.





Selecting paired parentheses



▍Adding a blank line after input or output



If you need to improve the readability of the code, you can make sure that empty lines are automatically added after the output or input data.





Improving the readability of the code due to empty lines



In order to enable this feature, you need to call the menu with the key F2



, enable the options Blank line after input



and Blank line after output



.





Enabling Blank line after input and Blank line after output



Highlighting syntactic constructions



Ptpython supports syntax highlighting, among other things.





Syntax highlighting



To switch between color themes, you can use the arrow key by



pressing it until a suitable theme is selected.



There are 39 topics in the system. If, for example, you want to choose the same color scheme that is used in Sublime Text - know that it has a code monokai



. This code must be entered in the menu options Code



found in the section Colors



.





Setting the theme in the menu



IPython magic commands



Ptpython supports IPython magic commands. In order to access the features of IPython, you need to use the command ptipython



.





IPython features





IPython features



Ptpython setup



The changes that are made to the ptpython settings during operation disappear after the end of the session with the program.



The settings that are used in each session must be described in the file $XDG_CONFIG_HOME/ptpython/config.py



. On Linux, the path to it looks like ~/.config/ptpython/config.py



.



Here is a file that contains all those useful program settings that we talked about above.



Outcomes



In this article, we've covered just my favorite features of ptpython. And there are, in fact, much more of them. If you like ptpython, you will probably find something in it that will be useful for you.



Are you planning to use ptpython?








All Articles