pypa/pip · error · InstallationError

Could not open {kind} file: {exc}

Error message

Could not open {kind} file: {exc}

What it means

InstallationError from get_file_content when a requirements (-r) or constraints (-c) file referenced by a bare path cannot be opened. After exhausting URL schemes (http/https/file), pip treats the argument as a filesystem path; any OSError on open(url,'rb') is wrapped with a 'kind' label ('requirements' or 'constraints') so the user knows which file type failed.

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 d7d0d0a394)

Solutions

  1. Confirm the file exists and is readable from the cwd pip is running in: 'ls -l <path>'.
  2. Use an absolute path or the correct relative path.
  3. If the error is a permission issue, fix it with chmod/chown.
  4. For nested '-r', remember paths resolve relative to the file containing the '-r' line.

Example fix

# before
pip install -r requirments.txt   # typo

# after
pip install -r requirements.txt
Defensive patterns

Strategy: validation

Validate before calling

import os
def requirement_file_ok(path: str) -> bool:
    return os.path.isfile(path) and os.access(path, os.R_OK)

Type guard

def is_openable_requirements(path: str) -> bool:
    return requirement_file_ok(path)

Try / catch

import os
path = 'requirements.txt'
if not os.path.isfile(path):
    print(f'missing {path}'); raise SystemExit(1)
run_pip(['install', '-r', path])

Prevention

When it happens

Trigger: 'pip install -r missing.txt' (file does not exist), '-c constraints.txt' where constraints.txt is unreadable, or a nested '-r sub/requirements.txt' from inside another requirements file whose path is wrong. Also fires for permission errors and broken symlinks.

Common situations: Typo in the requirements filename; running pip from the wrong cwd; CI that did not check out the file; permission/ownership mismatch; relative path that resolves differently in a container.

Related errors


AI-assisted analysis of pypa/pip@d7d0d0a394 (2026-08-04). Data as JSON: /data/errors/7a65899c14048cef.json. Report an issue: GitHub.