The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

How to Run Pip From The Python Interactive Shell

Thu 25 August 2022    Al Sweigart

UPDATE March 22, 2023: I now recommend using the one-liner import pip; pip.main(['install', 'module_name_here']) module to install modules from the interactive shell rather than the instructions in this blog post. This code is easier to remember and works on Python versions 2.7 and 3.5+. The only downside is that it doesn't work on Python 3.4, in which case, use the instructions below. Here in 2023, however, the number of students working from 3.4 is near nonexistent so this is unlikely to be an issue.
UPDATE November 20, 2022: I've created the pipfromrepl module to streamline this process.

Installing Python modules with the pip tool is surprisingly hard to describe to beginners learning to code. There are several potential issues: multiple Python installations, virtual environments, PATH environment variable settings. You have to introduce command-line terminals and file system navigation, and the differences between Windows and Mac/Linux. However, there is a line of code you can run from the interactive shell to handle all this for you.

To install a Python module, run the following code from the interactive shell (that is, the thing with the >>> prompt):

import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', '--user', 'MODULE_NAME'])

If you're running the interactive shell from a virtual environment and you get an error like ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv., then remove the --user option:

import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'MODULE_NAME'])

It's generally not advisable to run code that some website tells you to without understanding what it does, so let's break down each section.

import sys, subprocess;

This imports the sys and subprocess built-in modules so that we can access the sys.executable variable and subprocess.run() function. These modules are in Python's standard library, so they are always already installed with Python.

The semicolon makes Python consider it the "end of the line" so that we can fit two Python instructions on the same line.

subprocess.run(

This function call simulates running a command from the Terminal or Command Prompt window, except it's now done from Python code.

sys.executable,

The sys.executable variable contains the file path of the Python interpreter that is currently running the interactive shell. This is especially useful if you have multiple Python installations or virtual environments and want to run the current Python interpreter you are using.

'-m', 'pip', 'install', 'MODULE_NAME'])

When you run the Python interpreter with the -m option, you are running a module (in our case, the standard pip module) as a script. Running python -m pip is the same thing as running the pip tool itself. The 'install', 'MODULE_NAME' options tells pip to download and install a module from the online Python Packaging Index. Your computer needs to be connected to the internet when you run these instructions.

After running these instructions, you should see the output from pip in the window. For example, when I installed the pyrect module the interactive shell looked like this:

>>> import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyrect'])
Collecting pyrect
  Using cached PyRect-0.2.0.tar.gz (17 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: pyrect
  Building wheel for pyrect (setup.py) ... done
  Created wheel for pyrect: filename=PyRect-0.2.0-py2.py3-none-any.whl size=11196 sha256=5b7fb5f14167a70c955fff3bb14eecfa557f07747f27a03b5c63834173a58f49
  Stored in directory: c:\users\al\appdata\local\pip\cache\wheels\25\80\fa\27bb4a1c2e21f64ec71390489d52e57b7cc8afbe79bd595c5e
Successfully built pyrect
Installing collected packages: pyrect
Successfully installed pyrect-0.2.0
CompletedProcess(args=['C:\\Users\\Al\\.virtualenvs\\deleteme-NM1HlCaN\\Scripts\\python.exe', '-m', 'pip', 'install', 'pyrect'], returncode=0)

To verify that the module installed correctly, run import MODULE_NAME. If no error message appears, the module has been successfully installed.

If you're teaching a Python class or running a Python workshop, using this Python code is a great way to avoid confusion when you need them to install a third-party module from PyPI. If you want to go into more detail about the command-line, environment variables, and similar topics, you can read Chapter 2 of my free book, Beyond the Basic Stuff with Python.


Learn to program for free with my books for beginners:

Sign up for my "Automate the Boring Stuff with Python" online course with this discount link.

Email | Mastodon | Twitter | Twitch | YouTube | GitHub | Blog | Patreon | LinkedIn | Personal Site