pypa/pip · error · InstallationError
Could not open file
Error message
Could not open {kind} file: {exc} What it means
pip could not open the requirements file or constraint file specified with -r or -c. The get_file_content() function tried to open the path as a local file and caught an OSError (file not found, permission denied, or other I/O error). The error message distinguishes between 'requirements' and 'constraint' based on the constraint flag.
Solutions
- Verify the file exists: 'ls -la requirements.txt'
- Check for typos in the filename and path
- Ensure the file is committed to version control for CI environments
- Use absolute paths or verify the working directory: 'pwd' before running pip
Defensive patterns
Strategy: validation
Validate before calling
import os
def requirements_file_exists(path: str) -> bool:
return os.path.isfile(path) and os.access(path, os.R_OK)
if not requirements_file_exists("requirements.txt"):
print("Requirements file not found or not readable — check path and permissions") Type guard
import os
def is_accessible_requirements_file(path: str) -> bool:
"""Type guard: True if path is a readable file."""
return os.path.isfile(path) and os.access(path, os.R_OK) Try / catch
from pip._internal.exceptions import InstallationError
from pip._internal.req.req_file import parse_requirements
try:
list(parse_requirements("requirements.txt", session))
except InstallationError as e:
if "Could not open" in str(e):
print("Verify the filename, path, and working directory")
else:
raise Prevention
- Verify the requirements file exists with 'ls' before running pip -r
- Use absolute paths in CI to avoid working-directory ambiguity
- Ensure requirements files are committed to version control
When it happens
Trigger: Running 'pip install -r requirements.txt' when requirements.txt does not exist. Using '-c constraints.txt' when the file is missing or unreadable. Any OSError from open() on a local requirements/constraint file path.
Common situations: Wrong filename or path. File not checked into version control so CI can't find it. Wrong working directory. Permission issues. File path with a typo or wrong extension.
Related errors
- Could not split options
- Error reading
- Error reading pylock file
- Invalid requirement
- not found. Cannot resolve '--group' option.
AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08).
Data as JSON: /api/errors/7a65899c14048cef.
Report an issue: GitHub.
Appendix: source
Thrown at src/pip/_internal/req/req_file.py:589
:param session: PipSession instance.
"""
scheme = urllib.parse.urlsplit(url).scheme
# Pip has special support for file:// URLs (LocalFSAdapter).
if scheme in ["http", "https", "file"]:
# Delay importing heavy network modules until absolutely necessary.
from pip._internal.network.utils import raise_for_status
resp = session.get(url)
raise_for_status(resp)
return resp.url, resp.text
# Assume this is a bare path.
try:
with open(url, "rb") as f:
raw_content = f.read()
except OSError as exc:
kind = "constraint" if constraint else "requirements"
raise InstallationError(f"Could not open {kind} file: {exc}")
content = _decode_req_file(raw_content, url)
return url, content
def _decode_req_file(data: bytes, url: str) -> str:
for bom, encoding in BOMS:
if data.startswith(bom):
return data[len(bom) :].decode(encoding)
for line in data.split(b"\n")[:2]:
if line[0:1] == b"#":
result = PEP263_ENCODING_RE.search(line)
if result is not None:
encoding = result.groups()[0].decode("ascii")
return data.decode(encoding)
View on GitHub (pinned to f399c37189)