How to Solve "No such file or directory: 'requirements.txt'" with pip
The error "Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'" when using pip install -r requirements.txt
means that pip can not find the requirements.txt
file in the current working directory, or at the specified path.
This guide explains the common causes of this error and provides clear solutions.
Understanding the Error
The pip install -r requirements.txt
command tells pip to install the Python packages listed in a file named requirements.txt
. This file is a standard way to specify project dependencies. The error message No such file or directory
means that pip
can't locate the requirements.txt
file where it expects to find it. By default, that location is the current working directory.
Common Causes and Solutions
Incorrect Working Directory
The most common cause is that your terminal (or command prompt) is not in the same directory as your requirements.txt
file. pip
looks for the file in the current working directory unless you provide a full path.
Solution: Use the cd
(change directory) command to navigate to the directory that contains your requirements.txt
file. Then run the pip install -r requirements.txt
command again.
# Example:
cd /path/to/your/project # Navigate to the correct directory
pip install -r requirements.txt
- This will change your current directory to where the
requirements.txt
file is located.
File Doesn't Exist / Typo in Filename
The requirements.txt
file might not exist, or there might be a typo in the filename.
Solution:
- Double-check that a file named
requirements.txt
(case-sensitive) actually exists in the directory you're in. Usels
(Linux/macOS) ordir
(Windows) to list the files in the current directory. - If the file doesn't exist, you need to create it.
- If there's a typo, correct it in the
pip install -r
command.
Incorrect Path
You might be providing an incorrect path to the requirements.txt
file. This is especially relevant if you're not in the same directory as the file.
Solution:
- If the file is in the same directory as your terminal's current location, just use
requirements.txt
(no path needed). - If it's in a different directory, provide the correct relative or absolute path. For example:
pip install -r ../myproject/requirements.txt # Relative path
pip install -r /home/user/myproject/requirements.txt # Absolute path (Linux/macOS)
pip install -r C:\Users\user\myproject\requirements.txt # Absolute path (Windows)
Using Wrong Command in Dockerfile
If you're getting this error inside a Dockerfile, the issue is often related to how you're copying the file or running the command. The working directory inside the Docker build context is important.
-
Incorrect:
# Dockerfile (INCORRECT)
RUN pip freeze > requirements.txt #Creates file at root level
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt # File is NOT in /app- The
requirements.txt
file is created, but not in the place where the next command attempts to read it.
- The
-
Correct (Option 1 - COPY before RUN):
# Dockerfile (CORRECT)
COPY requirements.txt /app/requirements.txt # Copy *before* installing
WORKDIR /app
RUN pip install -r requirements.txt -
Correct (Option 2 - Generate in the right place):
# Dockerfile (ALSO CORRECT)
WORKDIR /app
COPY . /app
RUN pip freeze > requirements.txt # Create it *inside* /app
RUN pip install -r requirements.txt
Creating a requirements.txt
File
If you don't have a requirements.txt
file, you need to create one. The standard way to do this is with pip freeze
:
pip freeze > requirements.txt
# Or with pip3, if needed:
pip3 freeze > requirements.txt
- This command is best used in a virtual environment, as
pip freeze
will save all installed packages and their versions into therequirements.txt
file.
Installing from a requirements.txt
File
Once you have a requirements.txt
file, install the dependencies with:
pip install -r requirements.txt
# Or, for Python 3:
pip3 install -r requirements.txt
# Or, more explicitly:
python -m pip install -r requirements.txt
python3 -m pip install -r requirements.txt
py -m pip install -r requirements.txt # On windows
Specifying a Path to requirements.txt
If requirements.txt
is not in your current directory, provide the path:
pip install -r /path/to/your/requirements.txt
pip3 install -r /path/to/your/requirements.txt # If using pip3
Conclusion
The No such file or directory: 'requirements.txt'
error is usually a simple issue of being in the wrong directory or having a typo in the filename or path.
By carefully checking your current working directory, the file's existence, and the path you're providing to pip
, you can quickly resolve this error.
Remember to create the requirements file if one doesn't already exist, and if using Docker, place the command at the correct place in your Dockerfile.