pip — Package Installer
pip is Python's standard package installer. It downloads and installs libraries — anything from small utilities to large frameworks — so you can use them in your own code without writing them from scratch. pip has shipped bundled with Python since version 3.4, so on any modern Python install, it's already available via the pip (or pip3) command.
PyPI: Where Packages Come From
By default, pip downloads packages from the Python Package Index (PyPI), hosted at pypi.org. PyPI is a public registry where anyone can publish a package under a name and version number. When you run pip install requests, pip looks up requests on PyPI, resolves its dependencies, downloads the right files for your platform, and installs them into your environment.
Installing Packages
pip install requests
This installs the latest version of requests (and any packages it depends on) into your current Python environment.
Installing a Specific Version
Sometimes you need an exact version — to match a production environment, or because a newer release breaks your code. Pin the version with ==:
pip install requests==2.31.0
Upgrading and Uninstalling
To bring an already-installed package up to its latest version, use -U (short for --upgrade):
pip install -U requests # equivalent, spelled out: pip install --upgrade requests
To remove a package entirely:
pip uninstall requests
Listing Installed Packages
pip gives you two related but differently-formatted views of what's installed. pip list prints a friendly, human-readable table:
pip list
pip freeze, on the other hand, prints output in the exact package==version format that a requirements.txt file expects — making it the tool you use to snapshot an environment for reproduction elsewhere:
pip freeze
The requirements.txt Workflow
Most Python projects track their dependencies in a requirements.txt file. The typical workflow has two halves: capturing the current environment's package versions, and later reinstalling those exact versions somewhere else (a teammate's machine, a CI runner, a production server).
# Snapshot everything currently installed into requirements.txt pip freeze > requirements.txt # Later, on another machine, reproduce that exact set of packages pip install -r requirements.txt
A typical requirements.txt file looks like this:
requests==2.31.0 flask==3.0.0 python-dotenv==1.0.0
Installing from GitHub or a Local Path
pip isn't limited to PyPI. You can install directly from a Git repository:
pip install git+https://github.com/user/repo.git
Or from a package sitting in a local directory — useful while developing your own library:
pip install ./my-local-package # "editable" install: changes to the source are picked up # immediately without reinstalling pip install -e .
The -e (editable) flag is especially handy when you're actively developing a package alongside a project that depends on it — edits to the source take effect immediately, with no reinstall step.
Common pip Commands
Command | Description |
|---|---|
pip install <package> | Install the latest version of a package |
pip install <package>==<version> | Install a specific version |
pip install -U <package> | Upgrade an already-installed package |
pip uninstall <package> | Remove an installed package |
pip list | Show installed packages in a readable table |
pip freeze | Show installed packages in requirements.txt format |
pip install -r requirements.txt | Install everything listed in a requirements file |
pip install -e . | Install the current directory as an editable package |
pip show <package> | Display metadata about an installed package |