Use ptipython, a better Python shell

Posted on Wed 21 December 2016 in Python

Most Python developers use a shell, or REPL (Read-Eval-Print-Loop) to quickly try out small bits of code and also for quick and dirty debugging.

Type python in your command line and hit enter. That should bring up the default Python shell. You should see something like this:

Python 3.5.2 (default, Sep 28 2016, 18:11:34)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

(If you've never done this before, try entering any Python code.)

That's very nice, but did you know there are other Python shells?

My favourite shell is called ptipython. It combines ipython with ptpython and it's very cool. See here for a screenshot:

The ptipython shell

The Github repository contains most of the information, but let me show you a few basics.

Installation

Simply enter pip install ptipython to install this.

Some cool features

The ptipython shell comes with autocompletion and suggestions. Press F2 (or cmd + F2) to bring up the settings for it. Alternatively, you could also configure ~/.ptpython/config.py.

Here's how it appears for me:

Autocompletion

Another cool feature, is that if you put ? after a symbol (an object, module, class, function, or method), it brings up some basic information like the docstring. If you put two question marks (??) it shows you the code:

Docstring and code

Use it with Django

If you do web development in Django, you could also replace the default Django shell with ptipython through django_extensions.

The way to do this is to install django_extensions by typing pip install django_extensions, and then adding django_extensions to INSTALLED_APPS. Then type this to bring up the shell:

python manage.py shell_plus --ptipython

Hope you found this useful. Enjoy!

Subscribe to receive free Python tips

Sometimes I keep my best content for my email subscribers. Subscribe now so that you don't miss out! You can unsubscribe at any time, and I will never spam you.
* indicates required

Comments