[Errno 2] No such file or directory: 'requirements.txt'

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# [Errno 2] No such file or directory: 'requirements.txt'

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.

error could not open requirements file

shell
pip install -r requirements.txt ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

# Common causes of the error

The most common causes of the error are:

  • Running pip install -r requirements.txt from a directory that doesn't contain your requirements.txt file.
  • Forgetting to generate a requirements.txt file.
  • Misspelling the name of the file.
  • Using an incorrect command to generate the file in Docker.

# Creating a requirements.txt file

If 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.

shell
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.

pip freeze output

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.

shell
pip list # ๐Ÿ‘‡๏ธ For Python 3 pip3 list

pip list output

# Installing the packages from your requirements.txt file

Once you have a requirements.txt file, you can install it with the following command.

shell
pip install -r requirements.txt # ๐Ÿ‘‡๏ธ For Python 3 pip3 install -r requirements.txt

install packages from requirements txt

The -r option recursively installs the packages in your requirements.txt file.

# Creating a requirements.txt file in your Dockerfile

If you use Docker, use the following command to create a requirements.txt file.

Dockerfile
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.

Dockerfile
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.

# Installing a requirements.txt file that is located in a different directory

If 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.

shell
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.

shell
find . -regex '.*requirements.txt$'

The command finds any files that end with requirements.txt, e.g. dev-requirements.txt, base-requirements.txt, etc.

find requirements files

You can then specify the correct path when running the pip install -r /path/to/requirements.txt command.

If you only specify 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.

shell
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.

shell
# ๐Ÿ‘‡๏ธ cd into a directory cd example_dir # ๐Ÿ‘‡๏ธ cd 1 directory up cd .. pip install -r requirements.txt pip3 install -r requirements.txt
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev