pip Python Packages

pip cheatsheet — install, upgrade, remove Python packages. pip install package, pip install -r requirements.txt, pip freeze > requirements.txt, pip list. Full reference.

6 min read

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., requests for HTTP, numpy for numerical operations).
  • Dependencies: Packages that a project or another package relies on to function. pip automatically 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 pip itself 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 requests
    

    Installs the requests library.

  • Install a specific version of a package:

    pip install numpy==1.23.5
    

    Installs exactly version 1.23.5 of numpy.

  • Install a minimum version of a package:

    pip install "django>=3.2"
    

    Installs django version 3.2 or higher.

  • Install a package within a version range:

    pip install "pandas<2.0,>=1.4"
    

    Installs pandas version 1.4 up to, but not including, 2.0.

  • Install from a requirements file:

    pip install -r requirements.txt
    

    Installs 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 requests
    

    Removes the requests library and its associated files.

  • Uninstall multiple packages:

    pip uninstall numpy pandas matplotlib
    

    Removes numpy, pandas, and matplotlib.

  • Uninstall from a requirements file:

    pip uninstall -r requirements.txt -y
    

    Uninstalls all packages listed in requirements.txt without prompting for confirmation (-y).

Listing and Checking Packages

  • List installed packages:

    pip list
    

    Shows all packages installed in the current environment.

  • List outdated packages:

    pip list --outdated
    

    Shows packages that have newer versions available.

  • Show information about an installed package:

    pip show requests
    

    Displays details about the requests package, including version, author, license, and location.

  • Check installed package dependencies:

    pip check
    

    Verifies that installed packages have compatible dependencies.

Managing Requirements Files

  • Generate a requirements.txt file from installed packages:

    pip freeze > requirements.txt
    

    Creates requirements.txt listing all packages and their exact versions in the current environment.

  • Generate a requirements.txt file excluding certain packages:

    pip freeze | grep -v "pytest" > requirements.txt
    

    Creates requirements.txt but excludes packages containing "pytest".

Downloading Packages

  • Download package wheels or source distributions without installing:

    pip download requests
    

    Downloads the requests package and its dependencies to the current directory.

  • Download to a specific directory:

    pip download -d /path/to/downloads requests
    

    Downloads requests and its dependencies into /path/to/downloads.

Package Index Configuration

  • Install from a specific index URL:

    pip install --index-url https://my.private.repo/simple/ requests
    

    Installs requests from a custom package index.

  • Install from a specific index URL with fallback to PyPI:

    pip install --extra-index-url https://my.private.repo/simple/ requests
    

    Tries to find requests on the custom index first, then falls back to the default Python Package Index (PyPI).

Other Useful Flags

  • --upgrade or -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, forcing pip to fetch packages from the index.

    pip install --no-cache-dir requests
    
  • --quiet or -q: Suppress output.

    pip install -q requests
    
  • --verbose or -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.txt
    

    Creates 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.txt
    

    Clones 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 -U
    

    A 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=requests
    

    Installs the requests package directly from its GitHub repository.

  • Installing packages from a local directory (for development):

    pip install -e /path/to/my/local/package
    

    Installs a local package in "editable" mode, meaning changes to the source code are immediately reflected without reinstalling.

Gotchas

  • pip vs pip3: On systems with both Python 2 and Python 3 installed, pip might refer to Python 2’s package manager, while pip3 refers to Python 3’s. Always use pip3 or python3 -m pip to 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 sudo on Linux/Mac, which is generally discouraged. Virtual environments avoid this.
  • requirements.txt pinning: Using pip freeze creates a requirements.txt with 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: pip prefers 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: pip caches 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.