IPython is a robust Python shell that handles indentation, syntax highlighting, tab completion, and more. In this tutorial, we will cover how to install IPython and walk through how to make use of some features it makes available. Python provides a default REPL or shell for users to run Python code in real-time and see the results. While this default REPL is extremely useful, it does not handle things like indentation, syntax, or highlighting. This is where Ipython shines.
- This tutorial is performed as the root user on a Liquid Web Self-Managed Ubuntu 18.04 server.
- IPython is a Python package. The installation instructions should be the same regardless of the underlying operating system as long as the system has a functioning Python 3 installation with the Python package manager pip.
Step #1: Install IPython
Installing IPython is straightforward because it is just a Python package. Install it by running the following command.
root@ubuntu18:~# pip3 install ipython
Step #2: Use IPython
Dropping into an IPython shell is much like dropping into a python shell. To do so, run the following command.
root@ubuntu18:~# ipython Python 3.6.9 (default, Apr 18 2020, 01:56:04) Type 'copyright', 'credits' or 'license' for more information IPython 7.16.1 -- An enhanced Interactive Python. Type '?' for help. In :
To begin, let’s create some variables to demonstrate the syntax highlighting that is built into IPython.
The variable names and values have different colors. This makes it simpler to differentiate between variable assignments and makes reading code in IPython easier than in the base Python shell.
View Object Attributes with Tab Completion
IPython allows for tab completion in several different contexts, including tab-completing variables created in the session, viewing the attributes and methods available to a given object. For example, enter list into the IPython shell and press tab a few times, and you will see this.
Here we see all the methods available to a list in Python. The list of attributes and methods can be navigated through with the arrow keys and pressing enter on any one of them will select it and close the dialog.
IPython makes finding information about a function or method simple by providing a means to display the function or method’s docstring using the `?` symbol.
This is especially helpful to deduce just what a particular method or function does or what it expects in terms of arguments.
Automatic Proper Indentation
Python cares about whitespaces, and especially indentations. Without the conventional indentation, a Python program will error out and not execute properly. IPython gracefully handles indentation for the user.
for x in range(5): print(x)
In the above example, the necessary indentation for this for loop to complete properly was automatically handled by IPython. The user doesn’t have to manually add in spaces to create the indentation to make this code work.
IPython makes it easy to access all the commands that have been previously used in the current session with the %history command. This makes it easy to grab something that was already executed and copy and paste it back into IPython to rerun it.
These previous commands can be copied directly from the output of %history and performed in IPython again.
These are just a few of the many conveniences and efficiency boosters made available through IPython. It is an extremely robust Python shell that makes it possible to easily and quickly get off and running with a quick test of an idea or script. For further information and an even more in-depth look at the features, IPython has to offer. For more information, check out the IPython documentation.
Our Support Teams are replete with talented Linux technicians and System administrators who have an intimate knowledge of multiple web hosting technologies, especially those discussed in this article.
If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone @800.580.4985, a chat or support ticket to assist you with this process.