How to Install Python Packages with pip

Install a Python package with pip, pin a specific version, install everything from requirements.txt, and upgrade or remove packages when needed.

You need a third-party library like requests in a Python project. pip is included with the official Python installers, so it is already on most machines.

Install a package

python3 -m pip install requests

On Windows, use py -m pip install requests.

Running pip as python3 -m pip instead of bare pip guarantees the package lands in the same Python that python3 runs. On machines with several Python versions, bare pip can belong to a different one, and the import fails even though the install succeeded.

Install a specific version

python3 -m pip install requests==2.32.3

Use >= for a minimum version instead:

python3 -m pip install "requests>=2.31"

The quotes stop the shell from treating > as a redirect.

Install from a requirements file

Most projects list their dependencies in requirements.txt, one per line:

requests==2.32.3
flask>=3.0

Install all of them at once:

python3 -m pip install -r requirements.txt

Check what is installed

python3 -m pip list
Package    Version
---------- -------
pip        24.2
requests   2.32.3

For details on one package, including where it is installed and what it depends on:

python3 -m pip show requests

Upgrade or uninstall a package

python3 -m pip install --upgrade requests

To remove a package:

python3 -m pip uninstall requests

pip asks for confirmation; add -y to skip the prompt. Uninstalling does not remove the package's dependencies.

If you get an "externally-managed-environment" error. Newer Linux distributions and Homebrew Python block installs into the system Python. The fix is a virtual environment: python3 -m venv .venv, then source .venv/bin/activate (.venv\Scripts\activate on Windows). Inside it, python3 -m pip install works normally and everything stays isolated to that project.

If you are starting a new project. Create the virtual environment first, before installing anything. Then python3 -m pip freeze > requirements.txt captures the exact versions so the project installs the same way on another machine.

More Coding how-tos