How to Install and Use IPython
What is IPython?
If you’ve previously downloaded Python, you probably start the Python interpreter by typing the following command into your computer’s terminal:
When the interpreter loads, you can run Python code using the interpreter in your terminal. Although this is helpful, the Python interpreter has its limitations. For example, the Python interpreter does not provide syntax highlighting, tab completion, proper indentation, and much more.
IPython is an alternative Python interpreter. It is an interactive shell used for computing in Python. It provides many more useful features over the more popular default Python interpreter. In this article, you’ll learn how to get started with IPython and use some of its features.
Where Can I Get IPython?
There are two ways to install IPython on your computer. If you already have Python installed, you can use
pip (Python’s package manager) to install IPython using the following command:
pip install ipython
We suggest installing IPython using the Python Anaconda distribution. When you install Python via Anaconda, you’ll automatically install IPython. If you install Python via Miniconda (a smaller version of Anaconda), you’ll have to install IPython using
conda (Anaconda’s package manager for data science packages) with the following command:
conda install ipython
If you’d like to learn more about Python installation, please refer to the following Codecademy resources:
How Do I Use IPython?
Great! At this point, you should have IPython installed on your computer. Using IPython is not much different from using the default Python shell. To use it, type the following command in your computer’s terminal:
After typing this command, your terminal will provide you with a few details about IPython, like the version, a description of IPython, and some commands you can enter to help you get started (
That’s it! You’ve loaded the IPython interpreter! Let’s take a look at some of its features.
What Are Some Features of IPython?
Now that you’ve installed IPython, you might be wondering: “How is this interpreter an improvement over the default interpreter?” In this section, we’ll discuss a handful of features that make IPython much easier to use over its default counterpart.
- Run Native Shell Commands
When you run an interpreter, the interpreter typically has its own built-in commands. These built-in commands often collide with native shell commands.
For example, if you were to start the Python interpreter (using
python) and then type
cd after the interpreter loads, an error would appear in your terminal. This error occurs because the Python interpreter does not recognize the command. The command
cd is native to your computer’s terminal, but not to the Python interpreter. IPython includes support for native shell commands like
ls, command history, and more.
- Syntax Highlighting
One of the first things you’ll notice about IPython is that it provides syntax highlighting, meaning it uses color to differentiate parts of Python code. Type
x = 10 into the terminal and notice how IPython highlights the code with different colors. This syntax highlighting is an improvement over the default Python interpreter and makes code much easier to read in the terminal.
- Proper Indentation
If you’ve used Python before, you know that it cares a lot about whitespace and indentation. IPython recognizes this and automatically provides you with proper indentation when you type Python code in the interpreter. Let’s take a look at an example.
Start the IPython interpreter by typing
ipython in your terminal.
Next, type the following code and press “Enter” (or “Return” on your keyboard):
for x in range(10):
Notice that after you press “Enter” (or “Return”) on your keyboard, IPython automatically provides the required indentation (four spaces) on the next line of code.
On the next line, type the following code:
To run the code, press “Enter” (or “Return”) twice . (The second “Enter” informs IPython that you are typing code and instructs IPython to execute the code.)
Voila! You should see the following output:
0 1 2 3 4 5 6 7 8 9
- Tab Completion
IPython also provides tab completion. Let’s take a look at an example using Python’s
str module. As a refresher, recall that the
str module provides some useful methods you can use on strings.
Start IPython in your terminal. Type the following code and then press “Tab” on your keyboard:
After pressing tab, you should see a list of methods supported by the
You can use the up and down arrow keys on your keyboard to navigate through the methods and select the one you’d like to use, or you can begin typing the name of the method you want to use and complete it by using “Tab”. This a vast improvement over the default interpreter!
Tab autocompletion is useful because it provides you with the list of all possible methods a specific module contains. With such a vast array of options, however, you might wonder: “What does this method do?” Once again, IPython to the rescue!
Type the following command into IPython and press “Enter” (or “Return”) on your keyboard:
You should see IPython provide you with the documentation for that method.
You can use
? at the end of nearly any command in IPython if you want more information on the command (or method, variable, etc.).
- Pasting Blocks of Code
Last, but certainly not least, IPython is excellent for pasting large amounts of Python code. You can grab any block of Python code, paste it into IPython, and the result should be properly indented code in the IPython interpreter. Neat!
These are only a handful of features that IPython provides. If you’d like to learn more about IPython, we recommend the following resource:
What Is Jupyter?
IPython also provides you with the Jupyter Notebook. The Jupyter Notebook is a web application that allows you to create documents that contain executable code, formulas and equations, data visualizations, and more.
If you installed Python using Anaconda, you already have the Jupyter Notebook installed. Run the following command to start the notebook web application:
If you installed Python using Miniconda, you’ll need to install the Jupyter Notebook using
conda first. Type the following command:
conda install jupyter
Next, start the Jupyter Notebook by running the following command (as previously described):
When you start the notebook, you’ll notice that a tab will open if you have a web browser open. It will run the Jupyter Notebook on a local port, such as
http://localhost:8888 (or some other port). You’ll notice that the notebook will list out the contents of your computer in a directory format. You can create new “notebooks” by clicking “New” and then selecting “Python 2” (or the version of Python you have installed).
We won’t explain all the details of the Jupyter Notebook in this article. But it is worth noting that with Jupyter, you can create shareable files that can support live code, charts, graphs, math, different forms of markup (Markdown, LaTeX, etc.), and much more.
To learn more about Jupyter Notebooks, visit the official Project Jupyter website:
IPython is an alternative Python interpreter that provides improvements over the default Python interpreter. These improvements include syntax highlight, proper indentation, documentation, and much more. With iPython, you can also use Jupyter notebooks to create reports that contain live code, charts, and more. If you’re serious about using Python for computing and data analysis, IPython is a phenomenal tool for you to use.