Summary

One of Python’s best features is its interactive interpreter that lets you test your code quickly. What if there was something even better than Python’s own interpreter? There is, and it’s called IPython.

What Is IPython?

IPythonis an alternative interactive interpreter to the standard Python interpreter that ships with the language.

The standard interpreter is useful for testing out small snippets of code and learning the language, but its functionality is basic. IPython adds a lot of features that are useful if you make much more extensive use of interactive Python. This includes people who use it for data science and data analysis. It includes lots of features that I’ll mention later, like syntax highlighting and magic commands. IPython adds a lot of creature comforts to make interactive Python easier, even if all you’re doing is using Python as a desk calculator.

iPython session in an Ubuntu WSL terminal.

One of IPython’s spinoffs is thepopular notebook interface Jupyter, which is popular in scientific computing. When you use a Python-based Jupyter notebook, you’re using IPython to run the Python code. It’s one reason that Python and Jupyter are seriously challenging proprietary scientific and engineering programs like Mathematica and MATLAB.

IPython is widely available through many Linux distribution package managers and inMamba, where it’s a key part of my development environment.

Defining a function to print “Hello,” followed by the function argument in Python, showing syntax highlighting.

Color and Highlighting Make Code Easier to See

The think that immediately gives IPython the edge over the standard Python interpreter is the use of color and syntax highlighting. IPython is in color, while the regular interactive Python is monochrome.

This color isn’t just for looks. If you write code in mostmodern text editors, the editor will use it to highlight elements of the language. This is known as “syntax highlighting.” It’s useful for spotting mistakes before you commit them to code. When you get used to it, you’ll wonder how you did without it. IPython’s syntax highlighting brings this experience to interactive Python.

IPython help on NumPy library.

One thing I appreciate about IPython’s syntax highlighting is that it automatically highlights parentheses and brackets. These are common in Python. How many times have you typed a line only to get an error message because you forgot to include the closing parenthesis or bracket? When you type the open parenthesis, Python will highlight it. When you move over the closing parenthesis, it will highlight the opening parenthesis that it belongs to. This is useful for nested parentheses. It’s easy to get confused without this feature.

Magic Commands Make Things Easier

Ipython includes “magic commands” that let you run commands on the system. They’re prefaced with a “%” (percent sign) character. A good example is the %cd command, which changes the working directory, similar to how a Linux shell command does.

For example:

This will go to the root directory in Unix-like systems. To prove it, you can run an external command by pretending it with an exclamation point (!) character. To run the pwd command to see the current working directory in IPython:

There are lots of other useful built-in magic commands, or simply “magics,” inthe official documentation.

NumPY mean function help in IPython.

IPython Offers Built-in Help

Another nice feature of IPython is its built-in help. If I need to figure out what a library function does, I can add a question mark (?) to what I want to know about to get a quick answer without having to search the web.

Since I work with data as a hobby, I use NumPy a lot. If I want a refresher on it, I can import NumPy and then get help on the object

IPython tab complete of the tips database.

I can also get help on a specific function. For example, to see the documentation on the “mean” method to find the arithmetic mean:

This saves a lot of time when coding, since I don’t have to interrupt my flow to move to the web browser.

IPython tab complete of tips dataframe.

IPython’s Tab Completion Makes Things Easier

Another feature that I’ve gotten used to on Unix-like shells including Bash and zsh is tab completion. When I type the first few letters of something, such as a command or a filename, the shell will attempt to fill it in. This saves a lot of typing, especially for long commands or filenames.

IPython has brought this feature to Python. I can press Tab for a list of commands, just like at the shell window. It also works with other objects.

External ping command executed in IPython.

I’ll demonstrate with an example I’ve covered in other articles, a popular dataset showing tips in New York City restaurants. I’ll set up a Pandas dataframe objectvia the Seaborn library, since it’s available there.

I can then start exploring this dataset. I can complete it by starting the words “tips” and then pressing Tab. I can also tab-complete methods, like the .head() method:

IPython logstart command.

I can also tab-complete the columns, such as “total_bill.” This makes IPython a major saver of typing time.

You Can Explore Objects

As mentioned earlier, the tab-completion and help commands work on objects as well as features of the Python language. Python as an object-oriented language, which means that it works based on data structures that embed the underlying data into the code as “objects,” where messages are passed via methods.

This can get complicated, but the IPython commands make it easy to understand the code I’m writing as well as to understand objects that are created by Python libraries. A good example of that was the tips dataframe object. To verify that, I can use the type command in IPython:

Many of these objects have lots of methods. With tab-completion, I know what’s available, again without having to look up documentation.

You Can Run System Commands in IPython

While you may run system commands in Python using the os.system method, it can be cumbersome if you use it regularly. You have to import it on every session. IPython lets you run system commands easily without having to exit IPython or import any libraries. I showed this earlier with the pwd command.

It’s useful for running lots of system commands. For example, I can run a ping command:

This is handy when I just have a terminal window with IPython loaded. If I want to run a system command, I don’t have to open up another terminal tab or window.

This mechanism is so useful that it’s even possible to use IPython as a system shell if you want to. The official documentation hasinstructions on how to do this.

You Can Log Your Sessions

IPython is not only useful for running commands, but you may save your commands to log files for review, recall, and sharing with other people.

You can use IPython to create a transcript of your session. To start a log with the –logfile option when launching IPython

Within IPython, you can use the %logstart magic command, which saves to ipython_log.py by default:

You can turn logging off and on with the %logoff and %logon commands

If you decide to turn your log back on, you may use the “logon” magic:

You could share your log with someone else, but a better way to show what you’re doing is likely a Jupyter notebook, which will allow you to mix in text and images along with your Python code.

you may view your own command history with the %history magic command, but a better way to view your command history is by the use of command-line editing, similar to the kind you’d find on most modern shells. To search backward, press Ctrl+r.

Python is great because it includes an interactive mode for learning the language and quickly testing out code ideas. IPython takes this capability to the next level with powerful features like syntax highlighting and magic commands. These features are why IPython is so useful for budding Pythonistas.