Pip and Requirements.txt

Python 8 min min read Updated: Mar 09, 2026 Intermediate
Pip and Requirements.txt
Intermediate Topic 1 of 8

Pip & Requirements.txt in Python (DevOps Tools)

Pip is the official package manager for Python used to install, update, and manage Python libraries and dependencies. It plays a critical role in Python development and DevOps workflows because it ensures that applications run with the correct dependencies.

The requirements.txt file is commonly used in Python projects to list all required packages and their versions. This allows developers and deployment environments to install the exact same dependencies needed for the application.

What is Pip?

Pip stands for Pip Installs Packages. It is used to download and install packages from the Python Package Index (PyPI), which is the official repository of Python libraries.

Pip allows developers to easily manage project dependencies.

Installing Pip

Most Python installations include pip by default. You can check whether pip is installed using the following command:

bash pip --version

If pip is not installed, it can be installed using:

bash python -m ensurepip --upgrade

Installing Python Packages

Pip allows installing packages directly from PyPI.

bash pip install requests

This command installs the requests library used for making HTTP requests.

Installing Specific Package Versions

Sometimes applications require specific package versions.

bash pip install django==4.2

This installs Django version 4.2.

Upgrading Packages

Pip can upgrade existing packages to the latest version.

bash pip install --upgrade flask

Uninstalling Packages

Packages can also be removed using pip.

bash pip uninstall requests

Listing Installed Packages

You can view all installed Python packages using:

bash pip list

What is requirements.txt?

The requirements.txt file contains a list of all Python dependencies required for a project. It helps maintain consistent environments across development, testing, and production systems.

Example requirements file:

text flask==2.3.2 django==4.2 requests==2.31.0

Installing Dependencies from requirements.txt

To install all dependencies listed in the file:

bash pip install -r requirements.txt

This installs all packages required by the application.

Generating requirements.txt Automatically

Pip can automatically generate a requirements file containing all installed dependencies.

bash pip freeze > requirements.txt

This is commonly used before deploying applications.

Using Virtual Environments with Pip

Virtual environments isolate project dependencies so that different projects can use different versions of packages.

bash python -m venv venv source venv/bin/activate

After activating the virtual environment, packages installed with pip will only affect that project.

Why requirements.txt is Important in DevOps

  • Ensures consistent environments across machines
  • Simplifies deployment automation
  • Used in Docker containers and CI/CD pipelines
  • Helps manage project dependencies efficiently

Using requirements.txt in Docker

In DevOps environments, Docker containers often install dependencies using the requirements file.

dockerfile COPY requirements.txt . RUN pip install -r requirements.txt

This ensures the container installs all required packages.

Real-World Example

In a Django or Flask project, the requirements file might include dependencies such as:

text django djangorestframework gunicorn psycopg2

These dependencies are installed during deployment to ensure the application runs correctly.

Best Practices

  • Always use virtual environments for projects
  • Specify package versions in requirements.txt
  • Regularly update dependencies
  • Avoid installing unnecessary packages

Conclusion

Pip and requirements.txt are essential tools in Python development and DevOps workflows. Pip allows developers to manage dependencies efficiently, while the requirements file ensures consistent environments across development and production systems.

Understanding how to use pip and requirements.txt is crucial for managing Python projects, deploying applications, and maintaining reliable software environments.

In the next tutorial, we will explore CI/CD for Python Applications and learn how to automate build, testing, and deployment pipelines.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators