Installing IPython — Learn IPython Day 1

Installing IPython — Learn IPython Day 1

Welcome to Day 1 of the Learn IPython in 10 Days mini-course! Today you'll learn how to install IPython using the pipx tool, open IPython, and access IPython's built-in help menu.

Learning Outcomes

By the end of today, you'll know:

  • How to install pipx
  • Why you should install IPython with pipx instead of pip
  • How to view pipx environments
  • How to install IPython with pipx
  • How to open IPython from your terminal
  • How to view IPython's built-in help

Pre-requisites

Before you start today's lesson, make sure you have a working Python installation on your computer.

  • Windows and macOS: I highly recommend installing Python with the official Python.org installer.
  • Linux: You may already have a Python installation that came with your computer. I strongly suggest that you avoid using your system's pre-installed Python and instead install the latest version of Python using your operating system's package manager or (if you're up for it) building Python from source.

You can find full instructions for installing Python — regardless of your operating system — at the link below.

Python 3 Installation & Setup Guide – Real Python
The first step to getting started with Python is to install it on your machine. In this tutorial, you’ll learn how to check which version of Python, if any, you have on your Windows, Mac, or Linux computer and the best way to install the most recent version in any environment.

Step 1: Install pipx

Like most Python packages, you can install IPython using the pip package manager. But, there's a better way!

In this course, you'll use pipx to install the IPython REPL.

GitHub - pypa/pipx: Install and Run Python Applications in Isolated Environments
Install and Run Python Applications in Isolated Environments - GitHub - pypa/pipx: Install and Run Python Applications in Isolated Environments

IPython is a tool that you'll want access to no matter what Python environment you're working in. (Don't worry if you don't know what I mean by "Python environment." You'll still benefit from using pipx.)

Why use pipx and not pip? pipx installs packages from PyPI in their own isolated environments, much like installing an app from an app store.

So, in a way, pipx turns Python packages into applications that you can use across your system and across environments.

Find the heading corresponding to your operating system to see instructions for installing pipx on your computer. Then continue on to Step 2.

Windows

You can use pip to install pipx on Windows. First, open your favorite Windows terminal. I like the new Windows Terminal app, but you can also use PowerShell or cmd.exe.

ℹ️
NOTE: You will not be installing pipx in a virtual environment. pipx is one of the few Python packages that you actually want in your base Python environment!

Once your terminal is open, make sure that you have the latest version of pip installed by typing the following command and pressing Enter:

$ python -m pip install -U pip
⚠️
NOTE: If you installed Python from the Windows app store, replace python with python3 in the above command and all commands following it.

Then install pipx:

$ python -m pip install --user pipx

You may see the following warning when you install pipx:

WARNING: The script pipx.exe is installed in `<USER folder>\AppData\Roaming\Python\Python3x\Scripts` which is not on PATH

If you do, then navigate to that folder in your terminal using the cd command:

$ cd <USER folder>\AppData\Roaming\Python\Python3x\Scripts

You'll need to replace <USER folder> with whatever you see in the warning's output.

Once you're inside of the folder, run the following command to get pipx put on your PATH so that you can use the pipx command everywhere you need to:

$ pipx ensurepath

If you didn't see a warning when installing pipx, you'll still need to run the pipx ensurepath command above, although you can do this without having to navigate to a new directory.

You're all set, so go ahead and skip to Step 2.

Linux

You can install pipx on Linux using pip.

ℹ️
NOTE: You will not be installing pipx in a virtual environment. pipx is one of the few Python packages that you actually want in your base Python environment!

First, open your terminal and make sure that you have pip installed:

$ python3 -m pip --version
pip 22.0.4
⚠️
NOTE: I use the pip3 command here to make sure that you're using Python 3. If you have multiple versions of Python installed, for example if you installed the latest version of Python to avoid using the system Python that came pre-installed on your computer, you may need to provide the full version in the command.

For example, you may need to replace python3 with python3.10 in the above command and all commands following it.

If the above command works and you see a version number printed to the console, you're good to go. If not, you may need to install some additional packages before installing pipx. For example, Debian and Ubuntu users may need to run the following command:

$ sudo apt install python3-venv python3-pip

Next, make sure that you have the latest version of pip installed by executing the following command:

$ python3 -m pip install -U pip

Now you can install pipx:

$ python3 -m pip install pipx

Once pipx is installed, run the following command to get pipx put on your PATH variable so that you can use the pipx command everywhere you need to:

$ pipx ensurepath

You're all set, so go ahead and skip to Step 2.

macOS

There are two ways to install pipx on macOS:

  1. Using pip
  2. Using homebrew (my preferred method)

If you want to use pip, then the instructions are the same as for Linux, except that you won't need to install python3-venv or python3-pip before getting started.

To use homebrew, first install homebrew by following the instructions on the homebrew website:

Homebrew
The Missing Package Manager for macOS (or Linux).

Next, open your favorite macOS terminal. I use the iTerm2 terminal emulator with Oh My Zsh installed.

Update homebrew and then install pipx using the following commands:

brew update
brew install pipx

Then run the following command to get pipx on your PATH so that you can use the pipx command everywhere you need to:

pipx ensurepath

You're all set, so skip ahead to Step 2.

Step 2: Check Your pipx Installation

Now that you have pipx installed, the first thing you want to do is close your terminal. Then re-open it and execute the following command:

$ pipx list
nothing has been installed with pipx 😴

If you see the output saying that you haven't installed anything with pipx, then you're all set and can go ahead and skip to Step 3.

If you don't see anything printed, or you get an error, go back to the installation section for your operating system and make sure you follow each of the steps. If you still have trouble, you can try checking the pipx troubleshooting page.

Once pipx is installed and you've verified that the pipx command works from your terminal, it's time to install IPython!

Step 3: Install IPython

Before you install IPython with pipx, check to see if you have IPython installed with pip:

$ python3 -m pip show ipython

If IPython is not installed, you'll see a warning message:

WARNING: Package(s) not found: ipython

However, if IPython is installed, you'll see output that looks something like this:

Name: ipython
Version: 8.2.0
Summary: IPython: Productive Interactive Computing
Home-page: https://ipython.org
Author: The IPython Development Team
Author-email: [email protected]
License: BSD
Location: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages
Requires: appnope, backcall, decorator, jedi, matplotlib-inline, pexpect, pickleshare, prompt-toolkit, pygments, setuptools, stack-data, traitlets
Required-by:

You have two choices:

  1. Remove IPython from your global Python environment using python3 -m pip uninstall ipython (note that this will not remove any of IPython's dependencies)
  2. Ignore this and continue to install IPython with pipx. You'll see a warning when you install IPython with pipx telling you that you already had IPython installed via pip, but the ipython command will now point to the pipx installed IPython and not the pip installed IPython.

You may want to simply ignore that IPython is already installed, just in case you have another package installed that depends on IPython.

To install IPython with pipx, execute the following command in your terminal:

$ pipx install ipython

If the installation was successful, you'll see the following output:

  installed package ipython 8.2.0, installed using Python 3.10.2
  These apps are now globally available
    - ipython
    - ipython3
done! ✨ 🌟 ✨

You can see the IPython environment that pipx created using the pipx list command, which should show output similar to the following:

$ pipx list
venvs are in /Users/damos/.local/pipx/venvs
apps are exposed on your $PATH at /Users/damos/.local/bin
   package ipython 8.2.0, installed using Python 3.10.2
    - ipython
    - ipython3

Congratulations 🎉

You now have IPython installed with pipx 👏👏👏

To prove that IPython was installed in an isolated environment, run pip list to see everything installed in your global Python environment. Here's what mine looks like:

As you can see, there's no ipython package installed there. Of course, if you already had IPython installed and didn't remove it before installing IPython with pipx, you'll still see it listed with pip list.

Step 4: Launch IPython

To check that everything is working correctly, I suggest closing your terminal at this point and then re-opening it. Then run the following command to check that that IPython is installed and working:

$ ipython --version
8.2.0

If you see a version number printed to your console, you should be good to go! If not, you may want to return to Step 3 and make sure you followed all of the steps.

⚠️
Don't worry if the version number you see printed is different from the one in this tutorial. At the time of writing, the latest version of IPython was 8.2.0, but this might have changed by the time you're reading this.

Now you're ready to launch IPython for the first time. Type ipython into your terminal and press Enter. You should see a screen that looks something like this:

The first thing you'll see is a welcome message that tells you which version of Python is being used by IPython. This looks similar to the welcome message displayed by the standard Python shell, except that the last line indicates that you are running IPython.

There are a few things that already look different from the standard Python shell:

  • Instead of the >>> prompt, you see the prompt In [1]:
  • The prompt is green!

IPython uses numbered prompts that act sort of like code cells. We'll learn more about this during Day 2. IPython also supports colors, including syntax highlighting for your Python code!

Step 5: Opening IPython's Help Menu

The first thing to try in IPython is the help menu. Type a ? into the prompt and press Enter. You'll see the following screen:

The help menu provides a lot of helpful information about IPython and how to use it. You can always come back to it by typing ? into a prompt and pressing Enter.

You can navigate the help page using the PgUp and PgDn keys to move between pages, or use the Up and Down arrow keys to move line-by-line.

When you are done with the help page, press q to exit. You only need to press q. There's no need to press Enter.

Step 6: Exit IPython

To exit the IPython REPL and return to your shell, type exit into a prompt and press Enter:

This is one of the differences between IPython and the standard Python shell. If you type exit into the plain ol' Python REPL, you'll get a reminder to use the exit() function or Ctrl+D to exit the REPL:

It's the little things in life, ya know?

Day 1 Activities

Now that you have IPython installed, use it everywhere today that you would have used the standard Python shell instead. Take some notes while you're at it:

  • What do you immediately like about IPython?
  • What did you struggle with while using it?

And while you're at it, if you have a virtual environment that you can try this out in, activate the virtual environment and then open IPython. What happens?

I hope you enjoyed today's lesson. See you tomorrow for Day 2 of Learn IPython in 10 Days.


Enjoying this course? Why not share it with a friend or colleague that you think will benefit from it? Just right-click on this link, copy the URL, and send it to them in an email!