Last modified: Feb 12, 2026 By Alexander Williams

Install Python Requirements.txt: A Complete Guide

Working on a Python project often means using code written by others. These external packages are called dependencies. Managing them manually is hard. The requirements.txt file solves this problem.

It is a simple text file. It lists all the packages your project needs. This guide will show you how to install from it. We will cover everything from basic commands to best practices.

What is a Requirements.txt File?

A requirements.txt file is a project manifest. It contains a list of Python packages. Each package is usually listed with a version number.

This file ensures consistency. Every developer or system can install the exact same versions. This prevents the "it works on my machine" problem.

Here is a simple example of its contents:


# This is a requirements.txt file
requests==2.28.1
numpy>=1.21.0
pandas
flask

Lines can specify exact versions (==), minimum versions (>=), or just the package name for the latest.

Prerequisites: Python and Pip

Before you start, you need two tools. First, you need Python installed. Second, you need pip, Python's package installer.

Pip usually comes with modern Python installations. Check if you have them by opening your terminal or command prompt.

Run these commands to check your versions:


python --version
pip --version

If you see version numbers, you are ready. If pip is not found, you may need to install it separately or reinstall Python.

Step 1: Use a Virtual Environment (Recommended)

Always install packages in a virtual environment. It is a self-contained directory for your project. It isolates your project's dependencies from your system-wide Python.

This prevents version conflicts between different projects. It is a fundamental best practice in Python development.

Create and activate a virtual environment with these commands:


# Create a virtual environment named 'venv'
python -m venv venv

# Activate it on Windows
venv\Scripts\activate

# Activate it on macOS/Linux
source venv/bin/activate

Your terminal prompt should change, showing (venv). This means you are now inside the isolated environment.

Step 2: Navigate to Your Project Directory

Your requirements.txt file must be in your current working directory. Use the cd command to navigate to your project folder.

For example, if your project is on your Desktop:


cd Desktop/my_python_project

Use the dir (Windows) or ls (macOS/Linux) command to list files. Ensure you see requirements.txt in the list.

Step 3: Install from Requirements.txt

Now you are ready to install. The command is simple. Use the -r flag with pip install.

The basic syntax is:


pip install -r requirements.txt

When you run this, pip reads the file. It downloads and installs each listed package from the Python Package Index (PyPI).

You will see output showing the progress. Here is an example for a file containing requests and numpy:


(venv) C:\my_project> pip install -r requirements.txt
Collecting requests==2.28.1
  Downloading requests-2.28.1-py3-none-any.whl (62 kB)
Collecting numpy>=1.21.0
  Downloading numpy-1.24.3-cp310-cp310-win_amd64.whl (14.8 MB)
Installing collected packages: numpy, requests
Successfully installed numpy-1.24.3 requests-2.28.1

The process is complete when you see "Successfully installed".

Step 4: Verify the Installation

It is good practice to verify the install. You can list all packages installed in your current environment.

Use the pip freeze command. It shows all packages with their exact versions.


pip freeze

The output should match the packages and versions from your requirements.txt file.

You can also test by running your Python script. Try importing one of the newly installed packages in the Python interpreter.


# Open Python in your terminal
python
>>> import requests
>>> import numpy
>>> print(requests.__version__)
2.28.1
# If no error appears, the installation was successful.

Creating Your Own Requirements.txt File

Often, you need to create this file for your own project. After installing packages in your virtual environment, you can export the list.

The pip freeze command is used for this. Redirect its output to a file named requirements.txt.


pip freeze > requirements.txt

This command creates (or overwrites) the requirements.txt file in your current directory. It contains a pinned version for every package, which is excellent for reproducibility.

Common Errors and Troubleshooting

Sometimes, the installation fails. Here are common issues and their fixes.

Error: "requirements.txt' not found"

This means pip cannot find the file. You are likely in the wrong directory.

Solution: Use the ls or dir command to check. Navigate to the correct folder containing the file.

Error: Permission Denied

You might see a permission error on macOS or Linux. This often happens if you try to install globally without sudo or outside a virtual environment.

Solution: Always use a virtual environment. If you must install globally, use sudo pip install (not recommended).

Version Conflicts

One package may require a version of another package that conflicts with your list. Pip will show a detailed error message.

Solution: You may need to adjust version numbers in your requirements.txt. Sometimes using pip install --upgrade on a specific package can help resolve conflicts.

Best Practices for Using Requirements.txt

Follow these tips for professional dependency management.

Always Use a Virtual Environment. This cannot be overstated. It keeps your projects clean and separate.

Pin Your Versions. Use exact versions (==) in your production requirements.txt. This guarantees the same environment everywhere.

Keep the File Updated. When you add a new package to your project, re-run pip freeze > requirements.txt to update the list.

Consider Separate Files. Large projects sometimes use two files: requirements.txt for production and requirements-dev.txt for development tools (like linters or testing frameworks).

Conclusion

Installing from a requirements.txt file is a core Python skill. The command pip install -r requirements.txt is simple but powerful.

Remember the key steps: use a virtual environment, navigate to the right folder, and run the install command. This ensures a consistent and reproducible setup for any Python project.

Mastering this process is your first step towards professional Python development. It streamlines collaboration and deployment. Now you can confidently share your projects and run others' code.