Table of Contents

    pip Introduction

    CHAPTER 28.1 · PYTHON PACKAGE MANAGEMENT

    pip Introduction

    Learn how pip helps Python developers install, upgrade, inspect, and remove third-party packages efficiently.

    Python includes a powerful standard library, but many projects require additional tools and libraries such as Requests, NumPy, Pandas, Django, or Flask.

    Instead of manually downloading and configuring these libraries, Python developers normally use pip. pip is the standard package installer for Python. It can install packages from the Python Package Index and other package indexes.

    Learning objective: By the end of this tutorial, you will understand what pip is, how it works, how to verify it, and how to perform essential package-management operations.

    Prerequisites

    Before working with pip, make sure the following requirements are met:

    What You Need

    • Python 3 should be installed on your computer.
    • Python should be accessible from the command line.
    • You should know how to open Command Prompt, PowerShell, Terminal, or another command-line shell.
    • An internet connection is normally required when downloading packages from an online package index.
    • Basic knowledge of Python imports is helpful but not mandatory.

    What is pip?

    pip is a command-line package installer for Python. It allows developers to install and manage reusable software distributions created by the Python community.

    A package may contain Python modules, command-line utilities, configuration files, compiled components, documentation, or other resources required by a Python application.

    Think of pip as a package delivery service

    You provide the name of a Python package. pip locates an appropriate distribution, downloads it, resolves its required dependencies, and installs everything into the selected Python environment.

    Why is pip Important?

    Modern software development depends heavily on reusable packages. pip makes it possible to add tested libraries to a project without building every feature from the beginning.

    Faster Development

    • Install reusable packages with a command.
    • Avoid manually copying library files.
    • Use established solutions for common requirements.

    Easier Maintenance

    • Upgrade installed packages when required.
    • Remove packages that are no longer needed.
    • Record project dependencies for another developer.

    Package, Module, Library, and Distribution

    These terms are closely related, but they do not always mean exactly the same thing.

    Term General Meaning
    Module Usually a Python file containing reusable variables, functions, or classes.
    Import Package A structure that organizes related Python modules under a common namespace.
    Library A general term for reusable code that provides particular capabilities.
    Distribution Package An installable project containing code and metadata. This is normally what pip installs.
    Important: The package name used during installation may occasionally differ from the module name used in an import statement.

    What is the Python Package Index?

    The Python Package Index, commonly called PyPI, is a public repository of software for the Python programming language.

    Package authors can publish their distributions to PyPI. Developers can then use pip to locate and install those distributions.

    BASIC PACKAGE FLOW
    Your CommandpipPackage IndexPython Environment

    Check the Python Installation

    Open your command-line application and verify that Python is available.

    Windows

    py --version

    Depending on the Python installation and system configuration, the following command may also work:

    python --version

    Linux or macOS

    python3 --version

    Example output:

    Python 3.13.5
    These commands must be entered in Command Prompt, PowerShell, Terminal, or another system shell. Do not enter them directly inside the standard Python interactive prompt.

    Check Whether pip is Available

    Use one of the following commands to verify that pip is available.

    Windows

    py -m pip --version

    Linux or macOS

    python3 -m pip --version

    If the python command identifies the required Python installation on your system, you may use:

    python -m pip --version

    Example output:

    pip 25.1.1 from C:\Python313\Lib\site-packages\pip (python 3.13)

    The output normally identifies the pip version, installation location, and associated Python version.

    Why Use python -m pip?

    You may see examples using the shorter pip command:

    pip install requests

    However, this tutorial primarily uses:

    python -m pip install requests

    The -m option asks the selected Python interpreter to run pip as a module. This makes the relationship between the Python interpreter and pip more explicit.

    Potentially Ambiguous

    • pip may point to another Python installation.
    • Multiple Python versions can create confusion.
    • The command may not be available through the system PATH.

    More Explicit

    • python -m pip uses the selected interpreter.
    • py -m pip is convenient on Windows.
    • python3 -m pip is common on Linux and macOS.

    Install Your First Package

    The general syntax for installing a package is:

    python -m pip install package_name

    For example, the following command installs the Requests distribution:

    python -m pip install requests

    On Windows, you may use:

    py -m pip install requests

    On systems where Python 3 is invoked with python3, use:

    python3 -m pip install requests

    Example installation output:

    Collecting requests
    Downloading requests-2.x.x-py3-none-any.whl
    Installing collected packages: requests
    Successfully installed requests-2.x.x
    The exact package version, dependency list, filename, and output may differ depending on the operating system, Python version, package index, and installation date.

    Use the Installed Package

    After installing the Requests package, it can be imported into a Python program.

    import requests
    
    response = requests.get(
        "https://example.com",
        timeout=10
    )
    
    print("Status code:", response.status_code)

    Possible output:

    Status code: 200

    A status code of 200 normally indicates that the HTTP request completed successfully. The actual result depends on network availability and the response from the requested server.

    Essential pip Commands

    The following commands cover the most common introductory package-management operations.

    Command Purpose
    python -m pip --version Displays information about the available pip installation.
    python -m pip install requests Installs a package and its required dependencies.
    python -m pip install requests==2.32.3 Requests the specified package version.
    python -m pip install --upgrade requests Requests an upgrade of an installed package.
    python -m pip uninstall requests Uninstalls the specified package.
    python -m pip list Lists installed packages.
    python -m pip show requests Displays metadata about the installed package.
    python -m pip freeze Produces an installed-package listing in requirements format.
    python -m pip check Checks whether installed packages have compatible dependencies.

    Install a Specific Package Version

    Package versions can be controlled by including a version specifier.

    Install an Exact Version

    python -m pip install requests==2.32.3

    Install a Minimum Version

    python -m pip install "requests>=2.30.0"

    Use a Version Range

    python -m pip install "requests>=2.30.0,<3.0.0"
    Quoting a requirement containing comparison operators is a useful cross-platform practice because some command-line shells assign special meaning to characters such as > and <.

    List Installed Packages

    Use the following command to display packages installed in the current Python environment:

    python -m pip list

    Example output:

    Package      Version
    ------------ -------
    pip          25.1.1
    requests     2.32.3
    setuptools   80.9.0

    The package names and versions shown on your computer will depend on your environment.

    Inspect an Installed Package

    The show command displays available metadata about an installed package:

    python -m pip show requests

    Example output:

    Name: requests
    Version: 2.32.3
    Summary: Python HTTP for Humans.
    Location: C:\project\.venv\Lib\site-packages
    Requires: certifi, charset-normalizer, idna, urllib3

    Upgrade an Installed Package

    pip normally leaves an already installed version unchanged unless an upgrade is explicitly requested.

    python -m pip install --upgrade requests

    The shorter -U option may also be used:

    python -m pip install -U requests

    Upgrade pip

    pip itself is also a Python package. It can generally be upgraded using the following command:

    Windows

    py -m pip install --upgrade pip

    Linux or macOS

    python3 -m pip install --upgrade pip
    In a managed workplace, school, or enterprise environment, package installation may be controlled by organizational security, licensing, or open-source usage policies.

    Uninstall a Package

    Use the uninstall command to remove an installed package:

    python -m pip uninstall requests

    pip will normally ask for confirmation before removing the package.

    Proceed (Y/n)?

    Enter Y and press Enter to continue.

    Understanding Package Dependencies

    A package may rely on other packages to perform its work. These supporting packages are called dependencies.

    1

    Direct Dependency

    A package that your project explicitly requests.

    Example Your project directly installs requests.
    2

    Transitive Dependency

    A supporting package required by one of your direct dependencies.

    Example pip may install supporting distributions required by requests.

    During installation, pip resolves the dependency requirements and selects package versions that satisfy the applicable constraints.

    Use pip Inside a Virtual Environment

    Installing every package into the system-wide Python environment can cause conflicts between projects. A virtual environment provides a separate Python environment for an individual project.

    Step 1: Create a Virtual Environment

    python -m venv .venv

    Step 2: Activate It on Windows Command Prompt

    .venv\Scripts\activate

    Step 2: Activate It on Windows PowerShell

    .venv\Scripts\Activate.ps1

    Step 2: Activate It on Linux or macOS

    source .venv/bin/activate

    Step 3: Install the Package

    python -m pip install requests

    Step 4: Deactivate the Environment

    deactivate
    A virtual environment helps keep each project's packages and versions separate from other projects.

    Save Installed Packages

    The freeze command can produce a list of installed packages in requirements format:

    python -m pip freeze

    Redirect the output into a file named requirements.txt:

    python -m pip freeze > requirements.txt

    Example requirements.txt:

    certifi==2025.4.26
    charset-normalizer==3.4.2
    idna==3.10
    requests==2.32.3
    urllib3==2.4.0

    Another developer can request installation of the listed dependencies using:

    python -m pip install -r requirements.txt

    Common pip Problems and Solutions

    1

    pip Command Not Found

    The standalone pip command is unavailable or is not included in the system PATH.

    Possible Error 'pip' is not recognized as an internal or external command
    Try This Use python -m pip --version or py -m pip --version.
    2

    No Module Named pip

    pip is not available in the Python installation selected by the command.

    Possible Error No module named pip
    Try This Run python -m ensurepip --upgrade if ensurepip is supported by that Python distribution.
    3

    Permission Denied

    The selected environment or installation directory cannot be modified by the current user.

    Avoid Do not immediately modify a system-managed Python installation without understanding the effect.
    Recommended Approach Create and activate a virtual environment, and then install the package inside it.
    4

    Package Not Found

    pip cannot locate a distribution that matches the requested name, version, platform, or Python version.

    Possible Error No matching distribution found
    Check Verify the package name, requested version, Python version, operating system, and configured package index.

    Package Installation and Security

    Installing a package introduces third-party code into your environment. Package selection should therefore be treated as a software-supply-chain decision, not simply as a command-line task.

    Safer Package-Management Practices

    • Confirm the exact package name before installation.
    • Review the package's documentation, ownership, release information, and source repository where appropriate.
    • Avoid installing an unfamiliar package merely because its name resembles a popular project.
    • Use virtual environments to limit conflicts between projects.
    • Record and review project dependencies.
    • Follow your organization's open-source, licensing, privacy, and security policies.
    • Never place passwords, tokens, or private credentials directly inside installation commands or publicly shared files.

    Risky and Recommended Practices

    Risky Practices

    • Installing every package globally.
    • Using the wrong pip installation.
    • Installing misspelled or unverified package names.
    • Ignoring dependency conflicts.
    • Sharing a project without dependency information.
    • Running privileged installation commands unnecessarily.

    Recommended Practices

    • Use one virtual environment for each project.
    • Use python -m pip for clarity.
    • Verify package names before installation.
    • Use pip check to identify dependency issues.
    • Maintain an appropriate dependency file.
    • Follow organizational security and licensing policies.

    Hands-On Practice

    Complete the following exercise in a test project:

    Practice Activity

    • Create a folder named pip-practice.
    • Create a virtual environment named .venv.
    • Activate the virtual environment.
    • Check the pip version.
    • Install the Requests package.
    • Display its package information using pip show.
    • List all installed packages.
    • Create a requirements.txt file.
    • Run pip check.
    • Deactivate the virtual environment.

    Suggested command sequence:

    python -m venv .venv
    
    # Activate .venv using the command for your operating system
    
    python -m pip --version
    python -m pip install requests
    python -m pip show requests
    python -m pip list
    python -m pip freeze > requirements.txt
    python -m pip check
    deactivate

    Knowledge Check

    1

    What is pip?

    pip is a command-line package installer used to install and manage Python distributions.

    2

    What is PyPI?

    PyPI is the Python Package Index, a public repository of software for the Python programming language.

    3

    How can you check the pip version?

    python -m pip --version
    4

    How can you install the Requests package?

    python -m pip install requests
    5

    Why should a virtual environment be used?

    It provides a separate Python environment so package installations and versions can be managed independently for a project.

    pip Quick Reference

    # Display pip information
    python -m pip --version
    
    # Install a package
    python -m pip install requests
    
    # Install an exact version
    python -m pip install requests==2.32.3
    
    # Upgrade a package
    python -m pip install --upgrade requests
    
    # Uninstall a package
    python -m pip uninstall requests
    
    # List installed packages
    python -m pip list
    
    # Show package information
    python -m pip show requests
    
    # Check dependency compatibility
    python -m pip check
    
    # Create requirements output
    python -m pip freeze > requirements.txt
    
    # Install from a requirements file
    python -m pip install -r requirements.txt

    Summary

    What You Learned

    • pip is the standard package installer for Python.
    • pip can install packages from PyPI and other supported sources.
    • python -m pip explicitly uses pip associated with the selected Python interpreter.
    • Packages can be installed, inspected, upgraded, listed, checked, and uninstalled.
    • Virtual environments help isolate project dependencies.
    • Dependency information can be stored in a requirements file.
    • Third-party packages should be reviewed before use.

    Key Takeaway

    pip connects a Python environment to the wider Python package ecosystem. Learning commands such as install, list, show, upgrade, check, and uninstall gives you the foundation required to manage Python project dependencies effectively.