What requirements.txt does and why you need it

A requirements.txt file is a plain text list of Python packages your project depends on, along with the specific versions. When you run the install command, Python downloads and sets up each package automatically instead of you installing them one by one by hand.

Think of it like a grocery list. Instead of telling someone "I need flour, then eggs, then sugar," you hand them a written list and they get everything at once. The file lives in your project folder and contains lines like requests==2.28.1 or django==4.1.0.

Most Python projects you read from GitHub or other sources include a requirements.txt file. Your job is to read it and install what it says your project needs.

Key Takeaways

  • Install packages from requirements.txt by running pip install -r requirements.txt in your terminal or command prompt.
  • The -r flag tells pip to read the file and install everything listed inside it.
  • You need Python and pip already installed on your computer before this command will work.
  • If the install fails partway through, fix the error and run the same command again — pip will skip packages already installed.
  • Create your own requirements.txt by running pip freeze > requirements.txt after you install packages manually.

Before you start: check that pip is installed

Pip is Python's package manager — the tool that actually downloads and installs packages. It comes with Python by default on most systems, but you should verify it exists before you try to use it.

Open your terminal (Mac or Linux) or command prompt (Windows). Type this command and press Enter:

pip --version

If you see a version number like pip 23.0.1 from /usr/local/lib/python3.11/site-packages/pip, you are ready to go. If you see "command not found" or "pip is not recognized," pip is not installed or not in your system path. On Windows, try py -m pip --version instead. On Mac or Linux, you may need to use pip3 instead of pip if you have both Python 2 and Python 3 installed.

The basic command to install from requirements.txt

Navigate to the folder where your requirements.txt file is located. In your terminal or command prompt, type:

pip install -r requirements.txt

The -r stands for "read file." Pip will open requirements.txt, read each line, and install that package at the version specified. This usually takes a minute or two depending on how many packages are listed and how large they are.

You will see output scrolling past showing each package as it downloads and installs. When it finishes, you will see a line that says "Successfully installed" followed by a list of package names. At that point, all your packages are ready to use in your Python code.

Navigating to the right folder in your terminal

If you are new to the command line, finding the right folder can be confusing. On Windows, open command prompt and type cd C:\Users\YourName\path\to\your\project, replacing the path with the actual location of your project folder. On Mac or Linux, open terminal and type cd /Users/YourName/path/to/your/project.

If you are not sure of the exact path, you can drag your project folder into the terminal window on Mac or Linux, and the path will appear automatically. On Windows, you can hold Shift, right-click the folder, and select "Open PowerShell window here" to open a terminal already in that location.

Once you are in the right folder, type ls (Mac or Linux) or dir (Windows) to list the files. You should see requirements.txt in the output. If you do not, you are in the wrong folder.

What to do if the install fails partway through

Sometimes a package will not install because of a missing system library, a network problem, or a version conflict. Pip will show an error message and stop. Read the error carefully — it usually tells you what went wrong.

Common issues include: a package that does not exist under that name, a version that was removed from the package repository, or a package that needs a C compiler installed on your system. If the error mentions a missing compiler, you may need to install build tools for your operating system before trying again.

Once you fix the problem, run the same pip install -r requirements.txt command again. Pip remembers what it already installed and will skip those packages, installing only the ones that failed before. You do not need to start over from scratch.

Creating your own requirements.txt file

If you have installed packages manually using pip install package_name and now want to save that list so someone else (or you on another computer) can install the same versions, use the pip freeze command.

In your terminal, type:

pip freeze > requirements.txt

This creates a new requirements.txt file in your current folder containing every package you have installed in your current Python environment, along with the exact version number. The > symbol means "write the output to a file instead of showing it on screen."

Open the file in a text editor to see what it looks like. You will see lines like requests==2.28.1 and flask==2.2.2. You can edit this file by hand if you want to remove packages you do not actually need, or change version numbers to allow flexibility (for example, changing requests==2.28.1 to requests>=2.28.0 to allow newer patch versions).

Virtual environments: keeping projects separate

As you work on more Python projects, you may find that different projects need different versions of the same package. A virtual environment is a separate folder on your computer where each project can have its own set of installed packages without interfering with others.

To create a virtual environment in your project folder, type:

python -m venv venv

This creates a folder called venv inside your project. To set up it, type source venv/bin/set up on Mac or Linux, or venv\Scripts\set up on Windows. Your terminal prompt will change to show the environment is active. Now when you run pip install -r requirements.txt, packages install only into this project, not into your system Python.

Using virtual environments is considered a best practice in Python development because it prevents version conflicts and keeps your system Python clean. Most Python projects you read will expect you to set up a virtual environment first.

Frequently Asked Questions

Do I need to use pip3 instead of pip?

On systems with both Python 2 and Python 3 installed, pip may point to Python 2 and pip3 to Python 3. Since Python 2 is no longer supported, use pip3 if pip --version shows a Python 2 version number. On most modern systems, pip already points to Python 3.

What if a package in requirements.txt does not exist?

Pip will show an error saying the package was not found. Check the spelling against the requirements.txt file — package names are case-sensitive on some systems. If the spelling is correct, the package may have been removed from the repository or renamed. Search the package name on PyPI.org to see if it still exists under a different name.

Can I install only some packages from requirements.txt?

You can edit the requirements.txt file and delete the lines for packages you do not want, then run the install command. Alternatively, you can install individual packages using pip install package_name without touching requirements.txt, though this defeats the purpose of having a shared list.

How do I know which version of a package to use?

The requirements.txt file specifies the version the project was tested with. Use that version unless you have a reason to change it. If you want to see what versions are available, visit PyPI.org and search for the package name. You can also use pip index versions package_name to list all available versions from the command line.

What does the error "No module named pip" mean?

Pip is not installed or not in your system path. Try python -m pip install -r requirements.txt instead, which runs pip as a Python module. If that does not work, you need to install pip. On Windows, run the Python installer again and check the box for pip. On Mac or Linux, use your package manager or follow the instructions at pip.pypa.io.