PythonModulesBeginner

`pip` basics

The standard package installer for Python, used to install, upgrade, and manage Python packages from PyPI and other sources.

Review the syntaxStudy the examplesOpen the coding app
pip install package_name
pip uninstall package_name
pip freeze > requirements.txt

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

The standard package installer for Python, used to install, upgrade, and manage Python packages from PyPI and other sources.

`pip` (Pip Installs Packages) is the de facto standard package manager for Python, used to install and manage software packages written in Python. It connects to the Python Package Index (PyPI), a vast repository of third-party Python libraries. Basic `pip` commands include `pip install ame` to download and install a package and its dependencies, `pip uninstall ame` to remove a package, `pip list` to show installed packages, and `pip freeze` to output a list of installed packages in a format suitable for `requirements.txt` files. `pip` is almost always used within a virtual environment to ensure project isolation. When `pip` installs a package, it typically places it in the `site-packages` directory of the active Python environment. Best practices involve activating a virtual environment before using `pip` to install project dependencies, using `pip install -r requirements.txt` to install all dependencies listed in a file, and regularly upgrading `pip` itself (`python -m pip install --upgrade pip`). It's also good practice to pin exact package versions in `requirements.txt` (e.g., `requests==2.28.1`) for reproducibility. The time complexity of `pip` operations depends on network speed, package size, and the number of dependencies, but the tool itself is optimized for efficiency. Edge cases include dependency conflicts, broken package installations, or network proxy issues, which `pip` often reports with informative error messages.

Quick reference

Syntax

pip install package_name
pip uninstall package_name
pip freeze > requirements.txt

See it in practice

Examples

1

Installing a package

# Ensure you are in an activated virtual environment
# pip install requests

# To verify installation:
# pip show requests

# Example output:
# Name: requests
# Version: 2.31.0
# Summary: Python HTTP for Humans.
# Home-page: https://requests.readthedocs.io
# Author: Kenneth Reitz
# ...
Output:
Name: requests Version: 2.31.0 Summary: Python HTTP for Humans. Home-page: https://requests.readthedocs.io Author: Kenneth Reitz License: Apache 2.0 Location: /path/to/myproject_env/lib/python3.x/site-packages Requires: certifi, charset-normalizer, idna, urllib3 Required-by:

`pip install <ame>` downloads the latest stable version of the package from PyPI and installs it into the current Python environment (preferably a virtual environment).

2

Uninstalling a package

# Ensure you are in an activated virtual environment
# pip uninstall requests

# Example output:
# Found existing installation: requests 2.31.0
# Uninstalling requests-2.31.0:
#   Would remove:
#     /path/to/myproject_env/lib/python3.x/site-packages/requests-2.31.0.dist-info/*
#     /path/to/myproject_env/lib/python3.x/site-packages/requests/*
# Proceed (Y/n)? Y
#   Successfully uninstalled requests-2.31.0

# To verify uninstallation:
# pip show requests
# Example output: WARNING: Package(s) not found: requests
Output:
Found existing installation: requests 2.31.0 Uninstalling requests-2.31.0: Would remove: /path/to/myproject_env/lib/python3.x/site-packages/requests-2.31.0.dist-info/* /path/to/myproject_env/lib/python3.x/site-packages/requests/* Proceed (Y/n)? Y Successfully uninstalled requests-2.31.0

`pip uninstall <ame>` removes the specified package and its associated files from the current Python environment. It typically asks for confirmation.

3

Generating and installing from `requirements.txt`

# 1. Install some packages
# pip install requests==2.31.0 beautifulsoup4==4.12.2

# 2. Generate requirements.txt
# pip freeze > requirements.txt
# (This creates a file like:)
# certifi==2023.7.22
# charset-normalizer==3.3.0
# beautifulsoup4==4.12.2
# idna==3.4
# requests==2.31.0
# soupsieve==2.5
# urllib3==2.0.7

# 3. To install these dependencies in a new environment:
# python -m venv new_env
# source new_env/bin/activate
# pip install -r requirements.txt
Output:
Collecting certifi==2023.7.22 Downloading certifi-2023.7.22-py3-none-any.whl (158 kB) ... (output continues for all packages) Successfully installed beautifulsoup4-4.12.2 certifi-2023.7.22 charset-normalizer-3.3.0 idna-3.4 requests-2.31.0 soupsieve-2.5 urllib3-2.0.7

`pip freeze > requirements.txt` lists all installed packages and their exact versions, which is crucial for reproducibility. `pip install -r requirements.txt` then installs all packages listed in that file.

Debug faster

Common Errors

1

`pip` command not found

Cause: The `pip` executable is not in your system's `PATH`, or Python itself is not correctly installed/configured.

Fix: Ensure Python is correctly installed. If `pip` is still not found, try running it as a module: `python -m pip install ...` (recommended approach).

# pip install requests # Command not found
# python -m pip install requests # Correct
2

PermissionError

Cause: Attempting to install packages globally (outside a virtual environment) without sufficient administrator/root privileges.

Fix: Always use `pip` within an activated virtual environment. If you *must* install globally, use `sudo pip install ...` (on Unix/macOS) or run your terminal as administrator (on Windows), but this is generally discouraged.

# pip install some_package # PermissionError: [Errno 13] Permission denied
3

Dependency conflicts

Cause: Trying to install packages that have incompatible version requirements for a shared dependency.

Fix: Use a virtual environment for each project. Carefully manage `requirements.txt` by pinning exact versions. Consider using dependency resolution tools like `pip-tools` or `Poetry` for complex projects.

# pip install packageA==1.0 packageB==2.0 # If packageA requires dependencyX<5 and packageB requires dependencyX>5

Runtime support

Compatibility

AnyN/APython 2.7.9+, 3.4+

Source: pip Docs

Common questions

Frequently Asked Questions

The standard package installer for Python, used to install, upgrade, and manage Python packages from PyPI and other sources.

`pip` command not found: Ensure Python is correctly installed. If `pip` is still not found, try running it as a module: `python -m pip install ...` (recommended approach). PermissionError: Always use `pip` within an activated virtual environment. If you *must* install globally, use `sudo pip install ...` (on Unix/macOS) or run your terminal as administrator (on Windows), but this is generally discouraged. Dependency conflicts: Use a virtual environment for each project. Carefully manage `requirements.txt` by pinning exact versions. Consider using dependency resolution tools like `pip-tools` or `Poetry` for complex projects.