Python pip and Package Management

Learn Python pip and Package Management with practical examples, explanations, best practices, and real-world coding techniques.

Python pip and package management are essential for working with third-party libraries in Python applications. Python includes a powerful standard library, but real-world projects often depend on external packages for web development, databases, HTTP requests, data processing, testing, automation, machine learning, and many other tasks.

pip is the standard package installer commonly used to install Python packages from package indexes and other supported sources. With pip, you can install dependencies, upgrade packages, remove packages, inspect installed versions, and generate dependency lists that can be used to recreate an application's environment.

Package management becomes especially important as projects grow. A developer may have dozens of dependencies, and each dependency can itself rely on other packages. Managing these dependencies explicitly helps prevent "works on my machine" problems and makes development, testing, deployment, and collaboration more predictable.

What Is pip?

pip is Python's package installer. It is commonly used from the command line to install and manage packages available from the Python package ecosystem.

python -m pip install requests

Using python -m pip is often a good practice because it explicitly runs pip through the Python interpreter you selected. This is particularly useful when multiple Python installations or environments exist on the same machine.

Common pip Commands

Command Purpose
python -m pip install package Install a package
python -m pip uninstall package Remove a package
python -m pip install --upgrade package Upgrade a package
python -m pip show package Display package information
python -m pip list List installed packages
python -m pip freeze Output installed packages in requirements-style form
python -m pip check Check installed packages for dependency conflicts

Installing a Python Package

The most common pip operation is installing an external package. For example, the requests package can be installed with:

python -m pip install requests

After installation, the package can be imported by Python code:

import requests

response = requests.get("https://example.com")

print(response.status_code)

The package name used by pip and the module name used in Python code are often related, but they are not guaranteed to be identical. Always check the package documentation when the import name is not obvious.

Installing a Specific Package Version

Sometimes a project requires a particular package version because the application has been tested against that version or because a compatibility requirement exists.

python -m pip install requests==2.32.5

Version constraints can also allow a range rather than one exact release.

python -m pip install "requests>=2.30,<3"

Version constraints are part of dependency management. A project should decide whether a dependency needs to be pinned exactly, constrained to a compatible range, or allowed to follow newer compatible releases.

Upgrading a Package

Packages can be upgraded with the --upgrade option.

python -m pip install --upgrade requests

Upgrading dependencies should be done deliberately in production projects. A newer package version can introduce behavioral changes, deprecations, or compatibility issues with other dependencies even when the package itself is still supported.

A good dependency workflow usually combines upgrades with tests so that changes are verified before deployment.

Uninstalling Packages

Packages that are no longer needed can be removed using uninstall.

python -m pip uninstall requests

Removing a package from one environment does not mean the package is removed from every Python environment on your machine. This is another reason environment isolation matters when managing project dependencies.

Listing Installed Packages

The pip list command displays packages installed in the current Python environment.

python -m pip list

This is useful when diagnosing dependency issues, verifying installations, or checking which packages are currently available.

Inspecting a Package with pip show

The show command provides metadata about an installed package, including its version and installation location.

python -m pip show requests

Package inspection is helpful when you need to confirm which version is installed or determine where the package is located in the current environment.

requirements.txt

A requirements.txt file is a common way to describe Python project dependencies. It allows a project to store its dependency requirements in source control and gives other developers or deployment systems a repeatable installation command.

requests==2.32.5
python-dotenv>=1.0,<2
pytest>=8,<9

Another environment can install the dependencies using:

python -m pip install -r requirements.txt

This is one of the simplest ways to make a traditional Python application's dependencies reproducible across development and deployment environments.

Generating requirements.txt with pip freeze

The pip freeze command outputs installed packages in a format that can be used as a requirements file.

python -m pip freeze

You can redirect the output into a file:

python -m pip freeze > requirements.txt

This captures the installed environment as package-version pairs. However, the command records what is installed in the environment, which may include packages that are not direct dependencies of your application. For larger projects, dependency-management tools or carefully maintained dependency files may provide a more intentional dependency specification.

Installing Multiple Dependencies

A project may depend on several packages. Instead of installing each package manually, list them in a requirements file.

# requirements.txt

requests==2.32.5
pytest>=8,<9
python-dotenv>=1.0,<2

Then install everything with one command:

python -m pip install -r requirements.txt

This makes onboarding a project much easier because developers do not need to discover and install dependencies manually.

Dependency Versions and Compatibility

Package versions matter because different releases can provide different behavior or support different Python versions and dependencies. A project should define dependency constraints deliberately rather than allowing production environments to change unexpectedly.

Version Specifier Meaning Example
== Exact version requests==2.32.5
>= Minimum version pytest>=8
< Upper version boundary pytest<9
Combined constraint Version range pytest>=8,<9

The right strategy depends on the project. Applications that require strict reproducibility may pin exact versions, while libraries often need broader compatibility ranges so their consumers can resolve dependencies alongside their own requirements.

Checking Dependency Problems

The pip check command can identify installed packages that have unsatisfied dependency requirements.

python -m pip check

This is useful after installing or upgrading packages, especially when an environment has been modified multiple times and dependency compatibility needs to be verified.

pip and Virtual Environments

Package installation should normally happen in an environment dedicated to the project rather than modifying the operating system's Python installation. Virtual environments provide isolation so that one project can use versions that are different from another project.

python -m venv .venv

After activating the environment, pip installs packages into that environment:

python -m pip install requests

Virtual environments are covered in more detail in the dedicated Python virtual environments topic. The important idea here is that pip manages packages in the Python environment associated with the interpreter running the command.

Upgrading pip

pip itself is also a Python package and can be upgraded when appropriate.

python -m pip install --upgrade pip

Using python -m pip ensures the pip command is associated with the selected Python interpreter. This is particularly helpful when multiple versions of Python are installed.

Installing Packages for Development Only

Many projects need dependencies for development and testing that are not required by the production application itself. A common example is a testing framework such as pytest.

python -m pip install pytest

In larger projects, it is useful to distinguish runtime dependencies from development dependencies using the project's dependency-management strategy. This helps keep production installations smaller and makes the purpose of each dependency clearer.

Installing Packages from a requirements File

A new developer setting up an existing project can usually create the project's Python environment and install its dependencies from the requirements file.

python -m pip install -r requirements.txt

This workflow is especially useful in CI/CD pipelines and deployment processes because dependency installation becomes a repeatable command rather than a manual checklist.

Practical Example: Setting Up a Small Project

A typical small Python application might use a virtual environment, install its dependencies, and record those dependencies for other developers.

python -m venv .venv

# Activate the environment first

python -m pip install requests python-dotenv

python -m pip freeze > requirements.txt

python -m pip check

The exact activation command depends on the operating system and shell. Once the environment is active, the same python -m pip workflow can be used to manage the project's packages.

Package Management in Production

Production dependency management should be predictable and repeatable. Deployments should not depend on a developer manually installing packages on a server. Instead, the dependency specification should be part of the project's source-controlled configuration or build process.

A common deployment flow is:

1. Define dependencies

Store required packages and version constraints.

2. Create the environment

Use the appropriate isolated Python environment.

3. Install dependencies

Install from the project's dependency specification.

4. Test

Run automated tests and dependency checks.

5. Deploy

Promote the verified environment through the deployment pipeline.

Common pip and Package Management Mistakes

Installing into the wrong Python

Multiple Python installations can cause a package to be installed into one interpreter while the application runs another. python -m pip helps associate pip with the selected interpreter.

Installing globally by accident

Global installations can cause projects to share incompatible dependencies. Use isolated project environments for application development.

Ignoring versions

Allowing dependencies to change without an intentional compatibility strategy can cause different developers or deployment environments to behave differently.

Blindly running pip freeze

A freeze file captures the installed environment, which may contain indirect or unrelated packages. Review dependency files intentionally when project reproducibility matters.

Upgrading without testing

Dependency upgrades can change behavior or compatibility. Run the project's test suite after meaningful package upgrades.

Not recording dependencies

A project that depends on packages without documenting them is difficult for another developer or deployment system to reproduce.

Best Practices for Python Package Management

  • Use python -m pip so package installation is tied clearly to the intended Python interpreter.
  • Install project dependencies inside an isolated environment rather than relying on global packages.
  • Keep project dependencies documented in a source-controlled dependency file or appropriate project configuration.
  • Choose version constraints deliberately based on the application's compatibility and reproducibility requirements.
  • Use pip check when you need to verify that installed dependency requirements are satisfied.
  • Test important dependency upgrades before deploying them to production.
  • Avoid unnecessary third-party dependencies when the Python standard library already provides a suitable solution.
  • Review generated dependency lists instead of assuming every installed package is a direct application dependency.
  • Keep development-only dependencies separate from runtime dependencies when the project structure supports that distinction.
  • Make dependency installation part of the project's setup and deployment process rather than relying on manual server configuration.

Why pip and Package Management Matter

Python package management is a core part of professional Python development. Installing a library is easy, but maintaining a reliable application requires much more than running pip install. Dependencies need to be identified, versioned, isolated, tested, and reproduced consistently across development and deployment environments.

Understanding pip commands such as install, uninstall, show, list, freeze, and check gives you the foundation for managing Python dependencies confidently.

The most important principle is reproducibility. Another developer should be able to clone your project, create the appropriate environment, install the declared dependencies, run the tests, and obtain a predictable application setup without depending on packages that happen to exist on your machine.

Code Examples

Install a Package

python
# Install a package
python -m pip install requests

# Verify the installed package
python -m pip show requests
    

Version Constraints

python
# Exact version
python -m pip install "requests==2.32.5"

# Version range
python -m pip install "pytest>=8,<9"
    

requirements.txt

python
# requirements.txt

requests==2.32.5
python-dotenv>=1.0,<2
pytest>=8,<9

# Install all project dependencies:
# python -m pip install -r requirements.txt
    

Generate and Check Dependencies

python
# Save installed packages
python -m pip freeze > requirements.txt

# Check for dependency conflicts
python -m pip check
    

Basic Project Setup

python
# Create an isolated environment
python -m venv .venv

# Install project dependencies
python -m pip install requests python-dotenv

# Record installed packages
python -m pip freeze > requirements.txt
    

Pro Tip

Use python -m pip with an isolated project environment, keep dependencies explicitly documented, and test package upgrades before production deployment. Reproducible dependencies are more important than simply having the latest versions.

More Python lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.