Skip to content
FLAVIO COPES
flaviocopes.com

Install third-party Python packages with pip

By

Learn how to install third-party Python packages from PyPI using pip, from pip install to upgrading, pinning a version, uninstalling, and showing details.

~~~

To install a third-party Python package, you use pip, the package installer that ships with Python. You run pip install followed by the package name, and the package gets downloaded from PyPI and installed on your system.

The Python standard library contains a huge number of utilities that simplify our Python development needs, but nothing can satisfy everything.

That’s why individuals and companies create packages, and make them available as open source software for the entire community.

Those modules are all collected in a single place, the Python Package Index available at https://pypi.org, and they can be installed on your system using pip.

There are more than 270.000 packages freely available, at the time of writing.

You should have pip already installed if you followed the Python installation instructions.

How to install a package

Install any package using the command pip install:

pip install <package>

For example you can install the requests package, a popular HTTP library:

pip install requests

and once you do, it will be available for all your Python scripts, because packages are installed globally.

The exact location depends on your operating system.

On macOS, running Python 3.9, the location is /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages.

Upgrading, pinning, uninstalling

Upgrade a package to its latest version using the -U flag:

pip install -U requests

Sometimes you need a specific version, because the latest one breaks something or your project was tested against it. Install a specific version of a package using ==:

pip install requests==2.31.0

Uninstall a package using:

pip uninstall requests

Show an installed package details, including version, documentation website and author information using:

pip show requests

This prints something like:

Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Location: /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages

The Location line is handy when you’re not sure where pip put the package.

When pip installs to the wrong Python

A common problem: you install a package, then Python says ModuleNotFoundError when you import it.

This usually happens when you have more than one Python version on your machine. The pip command in your PATH belongs to one interpreter, but you run your script with another.

The fix is to run pip through the interpreter you actually use:

python -m pip install requests

This guarantees the package lands in the site-packages folder of that Python, the one that will run your script.

My advice is to prefer python -m pip whenever you’re in doubt. It removes the ambiguity entirely.

One more thing to keep in mind: installing everything globally works fine when you’re learning, but different projects will eventually need different versions of the same package. When you get there, look into virtual environments, which give each project its own isolated set of packages.

Tagged: Python · All topics
~~~

Related posts about python: