0
0
PythonHow-ToBeginner · 3 min read

How to Install Python Packages from requirements.txt File

To install Python packages from a requirements.txt file, use the command pip install -r requirements.txt. This tells pip to read the file and install all listed packages automatically.
📐

Syntax

The basic command to install packages from a requirements file is:

  • pip install -r requirements.txt

Here, pip is the Python package installer, install tells pip to add packages, and -r requirements.txt specifies the file listing the packages.

bash
pip install -r requirements.txt
💻

Example

This example shows how to create a requirements.txt file and install packages from it.

bash
echo -e "requests==2.28.1\nnumpy==1.24.2" > requirements.txt
pip install -r requirements.txt
Output
Collecting requests Downloading requests-2.28.1-py3-none-any.whl (62 kB) Collecting numpy Downloading numpy-1.24.2-cp39-cp39-manylinux_2_17_x86_64.whl (17.3 MB) Installing collected packages: requests, numpy Successfully installed numpy-1.24.2 requests-2.28.1
⚠️

Common Pitfalls

Common mistakes when installing from requirements.txt include:

  • Not running the command in the correct folder where the file is located.
  • Using an outdated version of pip that may not support some package versions.
  • Forgetting to activate the correct Python virtual environment before installing.
  • Typos or incorrect package names in the requirements.txt file.

Always check your current directory and update pip with pip install --upgrade pip if needed.

bash
Wrong way:
pip install requirements.txt

Right way:
pip install -r requirements.txt
📊

Quick Reference

Summary tips for installing from requirements.txt:

  • Use pip install -r requirements.txt to install all packages listed.
  • Make sure you are in the directory containing the requirements.txt file.
  • Activate your virtual environment before installing to keep packages isolated.
  • Update pip regularly to avoid compatibility issues.

Key Takeaways

Use pip install -r requirements.txt to install all packages listed in the file.
Always run the command in the folder where requirements.txt is located.
Activate your Python virtual environment before installing packages.
Keep pip updated to avoid installation errors.
Check the requirements.txt file for correct package names and versions.