{"record":{"id":"7a65899c14048cef","repo":"pypa/pip","slug":"could-not-open-kind-file-exc","errorCode":null,"errorMessage":"Could not open {kind} file: {exc}","messagePattern":"Could not open (.+?) file: (.+?)","errorType":"validation","errorClass":"InstallationError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/req/req_file.py","lineNumber":589,"sourceCode":"    :param session:     PipSession instance.\n    \"\"\"\n    scheme = urllib.parse.urlsplit(url).scheme\n    # Pip has special support for file:// URLs (LocalFSAdapter).\n    if scheme in [\"http\", \"https\", \"file\"]:\n        # Delay importing heavy network modules until absolutely necessary.\n        from pip._internal.network.utils import raise_for_status\n\n        resp = session.get(url)\n        raise_for_status(resp)\n        return resp.url, resp.text\n\n    # Assume this is a bare path.\n    try:\n        with open(url, \"rb\") as f:\n            raw_content = f.read()\n    except OSError as exc:\n        kind = \"constraint\" if constraint else \"requirements\"\n        raise InstallationError(f\"Could not open {kind} file: {exc}\")\n\n    content = _decode_req_file(raw_content, url)\n\n    return url, content\n\n\ndef _decode_req_file(data: bytes, url: str) -> str:\n    for bom, encoding in BOMS:\n        if data.startswith(bom):\n            return data[len(bom) :].decode(encoding)\n\n    for line in data.split(b\"\\n\")[:2]:\n        if line[0:1] == b\"#\":\n            result = PEP263_ENCODING_RE.search(line)\n            if result is not None:\n                encoding = result.groups()[0].decode(\"ascii\")\n                return data.decode(encoding)\n","sourceCodeStart":571,"sourceCodeEnd":607,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/req/req_file.py#L571-L607","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import os\n\ndef requirements_file_exists(path: str) -> bool:\n    return os.path.isfile(path) and os.access(path, os.R_OK)\n\nif not requirements_file_exists(\"requirements.txt\"):\n    print(\"Requirements file not found or not readable — check path and permissions\")","typeGuard":"import os\n\ndef is_accessible_requirements_file(path: str) -> bool:\n    \"\"\"Type guard: True if path is a readable file.\"\"\"\n    return os.path.isfile(path) and os.access(path, os.R_OK)","tryCatchPattern":"from pip._internal.exceptions import InstallationError\nfrom pip._internal.req.req_file import parse_requirements\n\ntry:\n    list(parse_requirements(\"requirements.txt\", session))\nexcept InstallationError as e:\n    if \"Could not open\" in str(e):\n        print(\"Verify the filename, path, and working directory\")\n    else:\n        raise","preventionTips":["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"],"tags":["pip","requirements-file","file-not-found","io-error","constraints"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}