avatarMartin Thoma

Summary

This article discusses four alternatives to the standard interactive Python shell, namely the standard shell enhanced by rich, IPython, bpython, and PtPython.

Abstract

The author begins by stating that Python's core advantage is its interactive shell, which is useful for exploratory tasks. However, the standard shell lacks features such as colorized output, autocompletion, and block history. The article then introduces four alternatives that offer these missing features. The first alternative is the standard shell enhanced by rich, a Python library for rich text and beautiful formatting in the terminal. The second alternative is IPython, which is known for its autocompletion and block history features. The third alternative is bpython, which has auto-completion and function definition help features. The final alternative is PtPython, which supports block history and has a different color theme than bpython. The author concludes by stating that they like bpython best but the missing block history is a deal-breaker for them, so they use IPython often.

Opinions

  • The author prefers bpython but the missing block history is a deal-breaker for them.
  • The author likes IPython's block history feature.
  • The author finds the missing block history in bpython to be an issue.
  • The author thinks that PtPython's color theme is not as good as bpython's.

4 Alternatives to the Standard Interactive Python Shell

The standard shell is ok, but there are four alternatives that offer missing features.

The default interactive shell of Python has no colors, no auto-completion, no block history. There are better options. Animation created by Martin Thoma.

One of the core advantages of Python compared to many other languages is that it comes with an interactive Shell.

This is especially useful for explorative tasks, e.g. exploratory data analysis or debugging. The standard shell is ok, but there are four alternatives that offer missing features. Let’s start!

1. Standard Shell enhanced by rich

Rich colorizes your output, but not your input. Image by Martin Thoma

Rich is a Python library for rich text and beautiful formatting in the terminal. It can be installed via pip install rich . Instead of using a different shell, you can make your shell execute those two lines within the standard shell:

from rich import pretty, inspect, traceback as tb
pretty.install(); tb.install(show_locals=False)

Once you’ve done that, you will be rewarded with colored output 🌈

2. IPython

IPython automatically colorizes your input, but not the output. It has a block-history as one can see in the end. Image by Martin Thoma

IPython is the interactive Python Shell is known by most from Jupyter Notebooks. It can be installed via pip install ipython . You start it over the console via ipython .

You will instantly recognize that the prompt changed from >>> to In [1] . Of course, you can customize it:

IPython gives you autocompletion triggered by Tab and colored errors:

IPython has a couple of advantages compared to the traditional console:

  • Colorized input and error messages: You have a much easier time seeing when you started a string or a number.
  • Autocompletion: It’s not super fancy, but at least you don’t have to type long strings all the time.
  • Block history: It’s pretty common that you want to go back to past input and modify/execute it again. For single-line input that’s fine, but if you have a function definition with nested loops the standard shell just shows you each line. IPython in contrast deals with the whole block as one input, meaning you have to press the up-arrow less often. This is a big deal for me.
  • IPython commands: You can use clear to get a clean window.

You can also configure Django to use the IPython shell. Of course, you can also install rich within the IPython shell to get colored output.

3. bpython

bpython is an interactive shell with tons of auto-completion features. It can be installed via pip install bpython and you start it with bpython . The auto-completion is triggered while you’re typing and gives astonishingly useful hints below your cursor in a box. The really crazy part is the function definition help:

Even better, that help adjusts to the size of your terminal:

You can see it best in an action:

Image from the README of bpython

To summarize, bpython has one core advantages compared to the default shell and IPython: Auto-completion / help text. This is a killer feature for me. It also has customization options and you can use bpython with Django.

However, there are also downsides:

  • Speed: It feels like there is a tiny delay when I type. It’s only a fraction of a second, but noticeable. I hate it.
  • Windows: Due to the usage of the curses library, I expect windows users to have issues using it. It seems to be possible, but bpython is not officially supported.
  • Missing Block history: This is an open issue since 2014.

4. PtPython

ptpython seems to have it all: colorized input and output, error message formatting, block history, function parameter help. Image by Martin Thoma

This looks pretty similar to bpython in the kind of features that are supported, but ptpython obviously uses a different color theme. In contrast to bpython, it supports block-history.

I don’t like the color theme, but you can change it by pressing “F2”. Sadly, something seems not to work as expected. They offer solarized dark, but it doesn’t look like solarized dark 😢

Summary

You can import rich in any interactive shell. For this reason, I didn’t include it in the following summary.

I like bpython best, but the missing block history (issue 22) is a deal-breaker for me. ptpython is the next best option, but I hate the theme. Hence I use IPython pretty often.

I love writing about software development and technology 🤩 Don’t miss updates: Get my free email newsletter 📧 or sign up for Medium ✍️ if you haven’t done it yet — both encourage me to write more 🤗

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

Python
Programming
Software Development
Shell
Coding
Recommended from ReadMedium