Chapters
Python114 chapters

Modules and the standard libraryChapter 71 of 114

PIP

Install packages other people wrote, and pin what your project needs.

What pip is

pip installs packages from PyPI, the Python Package Index. It comes with Python:

bash
python -m pip install requests

Use python -m pip rather than a bare pip. It guarantees you are installing into the interpreter you are about to run, which is the single most common cause of "I installed it but it says no module named requests".

Everyday commands

bash
python -m pip install requests
python -m pip install "requests==2.31.0"
python -m pip install --upgrade requests
python -m pip uninstall requests
python -m pip list
python -m pip show requests

Version specifiers

WrittenMeans
requestsany version
requests==2.31.0exactly that one
requests>=2.31that or newer
requests~=2.31.02.31.x, but not 2.32

Pin exactly for applications, so a deploy tomorrow installs what you tested. Be looser for libraries, so you do not force conflicts on whoever uses you.

requirements.txt

A plain list of what your project needs:

text
requests==2.31.0
python-dateutil==2.9.0
bash
python -m pip install -r requirements.txt
python -m pip freeze > requirements.txt

Always use a virtual environment

Installing into the system Python mixes every project's dependencies together and eventually breaks something. Make an environment per project:

bash
python -m venv .venv
source .venv/bin/activate      # Windows: .venv\Scripts\activate
python -m pip install requests

The Virtual Environments chapter goes into this properly. Treat it as the default, not an advanced option.

Checking what you have

Python
import sys
print(sys.executable != "")
print(sys.version_info >= (3, 10))

Output

True
True

sys.executable is the path to the interpreter that is running. When an import fails unexpectedly, printing it usually explains why: you installed into a different Python.

Before you install

Three questions worth asking, because every dependency is code you now rely on and have to keep updated:

  • Is it in the standard library already?
  • Is the package actively maintained?
  • Is the name exactly right? Typo-squatting on PyPI is real, and installing

reqeusts runs whatever that author wrote.

Import name and package name differ

There is no rule that they match:

text
pip install beautifulsoup4   ->   import bs4
pip install pillow           ->   import PIL
pip install python-dateutil  ->   import dateutil
pip install scikit-learn     ->   import sklearn

Packages in this editor

The examples on this page run under Pyodide in your browser, which has its own package mechanism rather than pip. Most of the standard library is there; large third-party packages are not, unless a page loads them deliberately.

Test yourself

2 questions

Why prefer 'python -m pip install' over a bare 'pip install'?

Show the answer

It installs into the interpreter you are about to run — Installing into a different Python is the usual cause of 'I installed it but the import fails'.

What does pip freeze write out?

Show the answer

Everything installed, including packages pulled in by other packages — It is a snapshot rather than a statement of intent, so keep a hand-written list of direct dependencies too.

Next chapter

Dates

Points in time, differences between them, and turning them into text.