Last updated: Apr 10, 2024
Reading timeยท3 min

The error "Could not open requirements file: [Errno 2] No such file or
directory: 'requirements.txt'" occurs when pip doesn't find a requirements.txt
file in the specified location.
To solve the error, create a requirements file with the
pip freeze > requirements.txt command.

pip install -r requirements.txt ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
The most common causes of the error are:
pip install -r requirements.txt from a directory that doesn't
contain your requirements.txt file.requirements.txt file.requirements.txt fileIf you don't already have a requirements.txt file, you can create one.
Open your terminal and run the following command to create a requirements.txt
file.
pip freeze > requirements.txt # ๐๏ธ For Python 3 pip3 freeze > requirements.txt
A requirements.txt file can only be generated from the output of the
pip freeze command.

You can also use the pip list
command to output all installed packages. However, the pip list command cannot
be used to generate a requirements.txt file.
pip list # ๐๏ธ For Python 3 pip3 list

requirements.txt fileOnce you have a requirements.txt file, you can install it with the following
command.
pip install -r requirements.txt # ๐๏ธ For Python 3 pip3 install -r requirements.txt

The -r option recursively installs the packages in your requirements.txt
file.
requirements.txt file in your DockerfileIf you use Docker, use the following command to create a requirements.txt
file.
RUN pip freeze > requirements.txt
If you have a requirements.txt file locally that you need to copy over to your
Docker image, use the following commands.
COPY requirements.txt /tmp/requirements.txt RUN python3 -m pip install -r /tmp/requirements.txt
The first command copies the requirements.txt file to the tmp directory of
the Docker image and the second command installs the requirements.txt file.
requirements.txt file that is located in a different directoryIf your terminal isn't located in the same directory as your requirements.txt
file, you can either navigate to the directory or specify an absolute path to
the requirements.txt file.
pip install -r /path/to/your/requirements.txt pip3 install -r /path/to/your/requirements.txt
You can use the following command if you need to find any files that end with
requirements.txt.
find . -regex '.*requirements.txt$'
The command finds any files that end with requirements.txt, e.g.
dev-requirements.txt, base-requirements.txt, etc.

You can then specify the correct path when running the
pip install -r /path/to/requirements.txt command.
requirements.txt when running the pip install -r command, make sure to open your terminal in the directory where your requirements.txt file is located.If the error persists, try adding quotes around requirements.txt.
pip install -r "requirements.txt" # ๐๏ธ For Python 3 pip3 install -r "requirements.txt"
If your terminal is open in a different directory use the cd command to
navigate to the directory that contains your requirements.txt file.
# ๐๏ธ cd into a directory cd example_dir # ๐๏ธ cd 1 directory up cd .. pip install -r requirements.txt pip3 install -r requirements.txt