FileNotFoundError: [Errno 2] No such file or directory: requirements.txt
Change your current working directory to the project folder (cd /path/to/project) or pass the full path: pip install -r /full/path/to/requirements.txt.
Root Cause Analysis
This error occurs when Python's package installer (pip) executes pip install -r requirements.txt, but cannot find a file named requirements.txt in the current terminal working directory.
1. Running Terminal Commands from the Wrong Directory
Opening a terminal defaults to your user home directory (C:\Users\username or /home/user). Running pip install -r requirements.txt without first navigating (cd) into your project repository causes pip to search the wrong folder.
2. Docker Container WORKDIR Misconfiguration
In a Dockerfile, executing RUN pip install -r requirements.txt before executing COPY requirements.txt . or before setting WORKDIR /app causes container builds to fail with Errno 2.
3. File Naming Typos or Hidden Extensions
On Windows, creating a text file in Notepad often names it requirements.txt.txt because Windows hides known file extensions by default.
4. Nested Folder Hierarchies
In multi-service or monorepo projects, requirements files may live inside requirements/base.txt or backend/requirements.txt.
Reproduction Code (MCVE)
from pathlib import Path
# Simulating pip searching for requirements.txt in wrong directory
req_file = Path('non_existent_directory/requirements.txt')
if not req_file.exists():
raise FileNotFoundError(f"[Errno 2] No such file or directory: '{req_file}'")
Solution 1: Navigate to Project Root or Provide Absolute Path
Verify the file location with ls or dir, navigate to the folder, or supply the absolute path to pip install -r.
import os
from pathlib import Path
print(f'Current Directory: {os.getcwd()}')
print('Navigate in terminal: cd /path/to/project')
print('Or run with explicit path: pip install -r /path/to/project/requirements.txt')
Solution 2: Correct Dockerfile COPY and WORKDIR Ordering
In Dockerfiles, copy the requirements file before invoking pip install to leverage Docker layer caching.
print('Recommended Dockerfile order:')
print('WORKDIR /app')
print('COPY requirements.txt .')
print('RUN pip install --no-cache-dir -r requirements.txt')
print('COPY . .')
A common mistake is using relative paths in automated CI/CD shell scripts without setting working directories. Always use $CI_PROJECT_DIR/requirements.txt or ${GITHUB_WORKSPACE}/requirements.txt in pipeline definitions. Edge cases occur with requirements inheritance: if dev.txt contains -r base.txt, running pip from outside the requirements/ folder requires running pip install -r requirements/dev.txt with relative path resolution. Contrast this error with pip._internal.exceptions.InstallationError, which occurs when a specific package listed inside the file fails to install.