IPython

Python REPL is nice. But it’s very basic and in the long run, it’s not convenient to use.

There are a few other alternatives. The most popular one is IPython - it’s the same REPL that runs behind the Jupyter Notebooks, so some of you are probably already familiar with it.

Left side: IPython. Right side: Python REPL

Installation

You can install IPython using pip or pipx:

$ pip install ipython
# Or even better
$ pipx install ipython

I recommend using pipx, because it’s one of those tools that you want to enable globally. You will use IPython in different projects, and you probably want the latest version anyway, instead of having a specific version per project.

Features

So what’s the difference between IPython and Python REPL? The best ones are:

  • Tab-completion - this is such a basic functionality, but pressing <Tab> in standard Python REPL will, well, insert a Tab (why would anyone want to put a tab after a function name?). Pressing <Tab> in IPython will trigger autocomplete suggestions, just like in your code editor.
  • Syntax highlighting
  • Automatic indentation - you no longer have to manually press space 4, 8, or 12 times when you are writing multiline functions.
  • Dynamic object introspection - this is my single, most favorite feature of IPython. You no longer need to search for documentation. Just append (or prepend) a question mark to the name of the function, module, or any Python object, and IPython will show you the docstring of that object.
    Do you want to see the complete source code? Use double question mark instead. You don’t have to search where a given function is located (even if it’s installed with pip) - IPython can give you this information right away.

  • Magic commands - functions that start with % or %%, that you can use to do things like:

    • Quickly measure the execution time of some code
    • Run a file
    • Browse the history of previous sessions
    • Rerun some previous commands
    • Open a code editor to edit some code before executing it
    • And more. You can also write your own magic functions if you want.

And that’s just a few of IPython’s features. If you want to learn more, the IPython documentation can give you a good overview.

If you prefer a more visual demonstration, I gave a presentation about IPython. It’s a 45 minutes long, fast-paced talk where I go through most of its features. Watching it on YouTube is probably better than seeing it live, as you can always stop and test a feature that you find interesting.

I recommend that you start using IPython instead of the default Python REPL. Even if you don’t care about most of the features, the tab-completion, syntax highlighting, and automatic indentation alone will make your life easier.

Alternatives

If you don’t like IPython, there are some other Python REPLs out there. They don’t have as many features as IPython, but they have different features - so maybe you will prefer one of them:

bpython

bpython in action

ptpython

ptpython in action