pip in Python: installing a library

pip installs a Python library from PyPI, dependencies included, into the interpreter it is attached to. Knowing which one is the hard part.
5 min read
Believemy logo

Definition

Writing a program rarely means writing everything yourself: part of the work already exists, published by somebody else as a library. What is left is getting it onto your machine, in the right place. That is what pip solves, the package installer shipped with Python.

It queries PyPI, the repository where most Python libraries are published, downloads the archive matching your version, drops it into the interpreter's folder, then does the same for every dependency. The most common form of the command looks like this:

BASH
pip install requests

From that single line, import requests works in your files. This replaces downloading an archive by hand, copying it to the right place, and hunting down the dependencies of the dependency. The requests library alone pulls four more in behind it, without you having to find each one yourself.

Good to know

pip is not limited to PyPI. It can also install from a Git repository or a local folder, mostly useful for trying a development version before its official release.


Trap number one: several Pythons on the machine

A computer rarely hosts a single Python: the system one, sometimes a second installed by hand, and a third per project virtual environment. What pip never does is guess which one you mean. It installs into the interpreter it is attached to at the moment you type the command, and nothing guarantees that this is the one which will run your script.

The symptom is always the same: the terminal announces "Successfully installed", and yet the import line raises a ModuleNotFoundError moments later. Nothing is broken, the library really is installed on disk, simply not in the interpreter running that script.

BASH
# Installation tied to the current interpreter
python -m pip install requests

# When in doubt, find out which is which
python -c "import sys; print(sys.executable)"

The python -m pip form guarantees that installation and execution go through the same interpreter, since it starts from Python itself rather than from a pip command that may point elsewhere. It is the first reflex when an installation seems to have achieved nothing.


The commands that actually matter

The pip documentation lists dozens of commands, reserved for specific cases. The table below gathers the seven that come up daily.

CommandWhat it does
pip install numpyInstalls the latest compatible version, dependencies included
pip install "numpy==1.26.4"Pins one exact version, to reproduce an environment
pip install -r requirements.txtReplays a dependency list, line by line
pip uninstall numpyRemoves the package, but leaves its dependencies behind
pip listShows what is installed in this precise interpreter
pip freezePrints the same list in dependency-file format
pip show numpyGives the version, the location on disk and the dependencies

The uninstall line deserves a second of attention: removing a package leaves behind everything it once dragged in. An environment is therefore never really cleaned dependency by dependency: it gets deleted entirely, then rebuilt from the dependency file.


What it does not do

pip installs, it isolates nothing. Without venv, everything lands in one shared installation, which causes no problem as long as a single project is involved. Trouble starts as soon as a second project, on the same machine, demands a different version of the same library: the two cannot coexist in the same place.

Warning

On a recent system, installing directly into the machine's Python returns an "externally-managed-environment" error. Forcing it through with --break-system-packages works, but mixes your libraries with the ones the operating system depends on to run: an unlucky update can break tools that have nothing to do with your project.

pip does not pin versions either. A requirements.txt file produced by pip freeze photographs the state of one machine at one moment: replaying that file six months later may well produce a different dependency tree.

This is the ground poetry and similar tools cover, handling the environment, the resolution and the locking in one place. Alone on a short script, pip plus a virtual environment are plenty. With several people on a project that must reinstall identically, a real lock file saves the evenings lost to a version mismatch.


Frequently asked questions

Question

Does pip need to be installed separately?

No, it has been part of the standard installation since Python 3.4. If the command still cannot be found in the terminal, it is almost never because it is truly missing: it is more likely that the bare pip command does not point to the expected interpreter. The python -m pip form fixes this in the vast majority of cases.

Question

Why do some names install something other than the expected library?

Because the name published on the repository does not always match the import name: you install scikit-learn to import sklearn. Worse, a standard library module such as json has nothing to install, and a similarly named package on the repository may point to something abandoned. The exact name always appears in the target project's documentation.

Question

How can every library be upgraded at once?

No official command does it, and that is a deliberate choice. Raising every version at the same time can break a project without saying which one is to blame. The most sensible route stays pip list --outdated, then one version bump at a time, each followed by a run of the test suite with pytest.

Related terms

Discover our python glossary

Browse the terms and definitions most commonly used in development with Python.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.