Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

FileNotFoundError: [Errno 2] No such file or directory: requirements.txt in Docker pip install

Verified FixPython 3.10+pip 23+ / DockerSilo: devops

Quick Fix / Solution Rapide

Copy requirements.txt into the container's WORKDIR with COPY requirements.txt . before running RUN pip install -r requirements.txt in your Dockerfile.

Root Cause Analysis

This error occurs when Python's package installer (pip) tries to read package specifications from a requirements.txt file, but the file does not exist in the current working directory of the container build environment or active terminal shell.

Docker Build Context & Layering

In a Dockerfile, each instruction (RUN, COPY, WORKDIR) executes in an isolated intermediate layer. A common beginner mistake is executing:

FROM python:3.12-slim
WORKDIR /app
RUN pip install -r requirements.txt  # FAILS! requirements.txt has not been copied yet
COPY . .

Because WORKDIR /app creates an initially empty directory inside the container image, running pip install -r requirements.txt looks for /app/requirements.txt, which does not yet exist.

Other Common Causes

  • Typo in File Name or Path: Naming the file requirement.txt (singular) or storing it in a subfolder (src/requirements.txt) without passing the relative path to pip install -r.
  • .dockerignore Exclusion: Accidentally listing requirements.txt or *.txt inside .dockerignore, preventing Docker from uploading the file into the build context.

Reproduction Code (MCVE)

Example: Bug Reproduction
# Simulates pip attempting to read a non-existent requirements file
target_file = 'nonexistent_requirements_file.txt'
with open(target_file, 'r', encoding='utf-8') as f:
    f.read()

Solution 1: Use Optimal Docker Layer Caching Order

Copy only requirements.txt first, install dependencies, and then copy the application source code to maximize Docker build cache efficiency.

Example: Recommended Solution
dockerfile_template = '''
FROM python:3.12-slim

# 1. Set container working directory
WORKDIR /app

# 2. Copy requirements first to leverage Docker layer caching
COPY requirements.txt .

# 3. Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# 4. Copy the rest of the application code
COPY . .

CMD ["python", "main.py"]
'''

print('Optimized Dockerfile structure:')
print(dockerfile_template.strip())

Solution 2: Support Nested Subfolder Requirements

When requirements are organized in a requirements/ directory (e.g. base.txt, prod.txt), copy the directory explicitly.

Example: Alternative Solution
import os
import tempfile

# Simulate multi-environment requirements layout
with tempfile.TemporaryDirectory() as tmpdir:
    req_dir = os.path.join(tmpdir, 'requirements')
    os.makedirs(req_dir, exist_ok=True)
    prod_req = os.path.join(req_dir, 'production.txt')
    with open(prod_req, 'w', encoding='utf-8') as f:
        f.write('fastapi>=0.100.0\nuvicorn>=0.22.0\n')
        
    print(f'Multi-environment requirements validated at: {prod_req}')

Common Pitfalls & Caching Best Practices

Copying COPY . . before RUN pip install -r requirements.txt is technically functional if the file exists, but it invalidates the Docker cache on every single line of code change, forcing pip to reinstall all packages from scratch on every docker build. Always copy requirements.txt as a standalone step.

Contrasting FileNotFoundError with pip.exceptions.DistributionNotFound: FileNotFoundError means the text file listing packages was not located; DistributionNotFound means the file was read successfully, but a specific package version listed inside could not be found on PyPI.