What it is
pip is the standard package manager for Python, used to install and manage libraries and dependencies for your Python projects.
Installation
Linux
sudo apt update
sudo apt install python3-pip
or for older Python versions:
sudo apt install python-pip
Mac
Python 3 usually comes with pip pre-installed. You can verify and upgrade:
python3 -m pip install --upgrade pip
If pip is not installed:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
Windows
Download get-pip.py from https://bootstrap.pypa.io/get-pip.py. Then run:
python get-pip.py
Make sure to add Python’s Scripts directory to your PATH environment variable.
Core Concepts
- Packages: Bundles of Python modules that provide specific functionality (e.g.,
requestsfor HTTP,numpyfor numerical operations). - Dependencies: Packages that a project or another package relies on to function.
pipautomatically handles installing these. - Virtual Environments: Isolated Python environments that allow you to manage dependencies for different projects separately. This prevents conflicts between project requirements. (Though
pipitself doesn’t create them, it’s used within them). requirements.txt: A file listing the specific packages and their versions required for a project.
Commands / Usage
Installing Packages
-
Install the latest version of a package:
pip install requestsInstalls the
requestslibrary. -
Install a specific version of a package:
pip install numpy==1.23.5Installs exactly version 1.23.5 of
numpy. -
Install a minimum version of a package:
pip install "django>=3.2"Installs
djangoversion 3.2 or higher. -
Install a package within a version range:
pip install "pandas<2.0,>=1.4"Installs
pandasversion 1.4 up to, but not including, 2.0. -
Install from a requirements file:
pip install -r requirements.txtInstalls all packages listed in
requirements.txt. -
Install packages without creating a virtual environment (not recommended for development):
pip install --user <package_name>Installs packages to the user’s site-packages directory, avoiding the need for root privileges.
Uninstalling Packages
-
Uninstall a package:
pip uninstall requestsRemoves the
requestslibrary and its associated files. -
Uninstall multiple packages:
pip uninstall numpy pandas matplotlibRemoves
numpy,pandas, andmatplotlib. -
Uninstall from a requirements file:
pip uninstall -r requirements.txt -yUninstalls all packages listed in
requirements.txtwithout prompting for confirmation (-y).
Listing and Checking Packages
-
List installed packages:
pip listShows all packages installed in the current environment.
-
List outdated packages:
pip list --outdatedShows packages that have newer versions available.
-
Show information about an installed package:
pip show requestsDisplays details about the
requestspackage, including version, author, license, and location. -
Check installed package dependencies:
pip checkVerifies that installed packages have compatible dependencies.
Managing Requirements Files
-
Generate a
requirements.txtfile from installed packages:pip freeze > requirements.txtCreates
requirements.txtlisting all packages and their exact versions in the current environment. -
Generate a
requirements.txtfile excluding certain packages:pip freeze | grep -v "pytest" > requirements.txtCreates
requirements.txtbut excludes packages containing "pytest".
Downloading Packages
-
Download package wheels or source distributions without installing:
pip download requestsDownloads the
requestspackage and its dependencies to the current directory. -
Download to a specific directory:
pip download -d /path/to/downloads requestsDownloads
requestsand its dependencies into/path/to/downloads.
Package Index Configuration
-
Install from a specific index URL:
pip install --index-url https://my.private.repo/simple/ requestsInstalls
requestsfrom a custom package index. -
Install from a specific index URL with fallback to PyPI:
pip install --extra-index-url https://my.private.repo/simple/ requestsTries to find
requestson the custom index first, then falls back to the default Python Package Index (PyPI).
Other Useful Flags
-
--upgradeor-U: Upgrade a package to the latest version.pip install --upgrade requests -
--no-deps: Do not install or uninstall dependencies.pip install --no-deps requests -
--force-reinstall: Reinstall the package even if it is already installed.pip install --force-reinstall requests -
--ignore-installed: Ignore existing installed packages when installing.pip install --ignore-installed numpy -
--no-cache-dir: Disable the cache, forcingpipto fetch packages from the index.pip install --no-cache-dir requests -
--quietor-q: Suppress output.pip install -q requests -
--verboseor-v: Increase output verbosity.pip install -v requests -
--dry-run: Show what would be done without actually doing it.pip install --dry-run requests
Common Patterns
-
Setting up a new project:
python -m venv venv source venv/bin/activate # On Linux/Mac # venv\Scripts\activate # On Windows pip install requests flask pip freeze > requirements.txtCreates a virtual environment, activates it, installs packages, and saves them to
requirements.txt. -
Cloning a project and installing its dependencies:
git clone https://github.com/user/repo.git cd repo python -m venv venv source venv/bin/activate pip install -r requirements.txtClones a repository, sets up a virtual environment, and installs dependencies from
requirements.txt. -
Upgrading all outdated packages:
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -UA common (though potentially risky) one-liner to upgrade all installed packages. It’s often better to manage upgrades on a per-project basis using
requirements.txt. -
Installing packages from a Git repository:
pip install git+https://github.com/psf/requests.git@main#egg=requestsInstalls the
requestspackage directly from its GitHub repository. -
Installing packages from a local directory (for development):
pip install -e /path/to/my/local/packageInstalls a local package in "editable" mode, meaning changes to the source code are immediately reflected without reinstalling.
Gotchas
pipvspip3: On systems with both Python 2 and Python 3 installed,pipmight refer to Python 2’s package manager, whilepip3refers to Python 3’s. Always usepip3orpython3 -m pipto ensure you’re managing packages for Python 3.- Global vs. Virtual Environment: Installing packages globally (outside a virtual environment) can lead to conflicts and make it difficult to manage dependencies for different projects. Always use virtual environments for development.
- Permissions: Installing packages globally often requires
sudoon Linux/Mac, which is generally discouraged. Virtual environments avoid this. requirements.txtpinning: Usingpip freezecreates arequirements.txtwith exact versions. While good for reproducibility, it can sometimes prevent upgrades to newer, potentially more secure or performant versions. Consider using version specifiers (e.g.,>=) for more flexibility if appropriate.- Wheel vs. Source Distribution:
pipprefers installing pre-compiled "wheel" files (.whl) for faster installation. If a wheel isn’t available for your platform, it falls back to compiling from a "source distribution" (.tar.gz,.zip), which might require development headers or compilers to be installed on your system. - Cache:
pipcaches downloaded packages to speed up subsequent installations. If you suspect issues with a downloaded package, you might need to clear the cache (pip cache purge) or use--no-cache-dir.