The Invent with Python Blog

Writings from the author of Automate the Boring Stuff.

A Quick Pip Guide: How to Install Python Modules

Mon 11 January 2021    Al Sweigart

Pip is a package manager that installs third-party packages (such as PyAutoGUI, Matplotlib, or Pygame) from https://pypi.org/, the Python Package Index. For a more detailed blog post on how pip works, see What Is Pip? A Guide for New Pythonistas

On Windows, to install a module named module_name open a Command Prompt window and run:

pip install --user module_name

On macOS and Linux, pip is for the older Python 2 version, so you need to open a Terminal window and run pip3 instead. On Ubuntu Linux, pip doesn't come with the pre-installed version of Python. You'll need to install it by opening a Terminal window and running sudo apt-get install python3-pip. (This AskUbuntu question has more information.)

pip3 install --user module_name

(If you get a 'pip' is not recognized as an internal or external command, operable program or batch file. or pip3: command not found error message, then the folder that the pip tool is listed in must be added to the PATH environment variable. You can find out about hte command line and environment variables in Chapter 2: Environment Setup and the Command Line in my free book, Beyond the Basic Stuff with Python.

If you can run python or python3 from the terminal/command prompt window without errors, you can use Python itself to run pip by entering the following on Windows:

python -m pip install --user module_name

On macOS and Linux, run:

python3 -m pip install --user module_name

Remember to run pip or python from the command line (either Terminal or Command Prompt) and not from the Python interactive shell. The Python interactive shell will have the >>> prompt. However, you can run pip from the Python interactive shell by running the following Python code:

import sys, os; os.system(sys.executable + ' -m pip install --user module_name')

Note the space before the -m is required. Replace module_name with the name of the module you wish to install, but type everything else exactly as shown.

You can find packages to install by browsing PyPI, the Python Package Index, at https://pypi.org/


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