{"record":{"id":"6c1c96a1e4bc7563","repo":"pypa/pip","slug":"cannot-read-filename-err","errorCode":null,"errorMessage":"cannot read {filename}: {err}","messagePattern":"cannot read (.+?): (.+?)","errorType":"exception","errorClass":"ClassNotFound","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pygments/formatters/__init__.py","lineNumber":111,"sourceCode":"\n    :exc:`pygments.util.ClassNotFound` is raised if there are any errors loading\n    the formatter.\n\n    .. versionadded:: 2.2\n    \"\"\"\n    try:\n        # This empty dict will contain the namespace for the exec'd file\n        custom_namespace = {}\n        with open(filename, 'rb') as f:\n            exec(f.read(), custom_namespace)\n        # Retrieve the class `formattername` from that namespace\n        if formattername not in custom_namespace:\n            raise ClassNotFound(f'no valid {formattername} class found in {filename}')\n        formatter_class = custom_namespace[formattername]\n        # And finally instantiate it with the options\n        return formatter_class(**options)\n    except OSError as err:\n        raise ClassNotFound(f'cannot read {filename}: {err}')\n    except ClassNotFound:\n        raise\n    except Exception as err:\n        raise ClassNotFound(f'error when loading custom formatter: {err}')\n\n\ndef get_formatter_for_filename(fn, **options):\n    \"\"\"\n    Return a :class:`.Formatter` subclass instance that has a filename pattern\n    matching `fn`. The formatter is given the `options` at its instantiation.\n\n    Will raise :exc:`pygments.util.ClassNotFound` if no formatter for that filename\n    is found.\n    \"\"\"\n    fn = basename(fn)\n    for modname, name, _, filenames, _ in FORMATTERS.values():\n        for filename in filenames:\n            if _fn_matches(fn, filename):","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/pygments/formatters/__init__.py#L93-L129","documentation":"Raised by load_formatter_from_file when open(filename) raises OSError (file missing, permission denied, not a file, etc.). The OSError is caught and re-raised as ClassNotFound with the original error message at __init__.py:110-111. This is a filesystem/permission problem, not a content problem.","triggerScenarios":"Passing a path that does not exist, a directory path, or a file without read permission to load_formatter_from_file; relative path resolved against an unexpected working directory.","commonSituations":"Wrong working directory; typo in path; file deleted between checks; read permissions stripped; passing a URL instead of a local path.","solutions":["Verify the path exists and is a readable file with os.path.isfile before calling.","Use an absolute path to avoid working-directory ambiguity.","Fix file permissions (chmod) if access is denied."],"exampleFix":"# before\nload_formatter_from_file('formatters/myfmt.py')\n# after\nimport os\np = os.path.abspath('formatters/myfmt.py')\nload_formatter_from_file(p)","handlingStrategy":"validation","validationCode":"import os\nif not os.path.isfile(path):\n    raise FileNotFoundError(f'not a readable file: {path}')","typeGuard":"def is_readable_file(path: str) -> bool:\n    import os\n    return os.path.isfile(path) and os.access(path, os.R_OK)","tryCatchPattern":"from pip._vendor.pygments.util import ClassNotFound\ntry:\n    fmt = load_formatter_from_file(path)\nexcept ClassNotFound as e:\n    if 'cannot read' in str(e):\n        raise FileNotFoundError(path) from e\n    raise","preventionTips":["Use absolute paths.","Check os.path.isfile + os.access before calling."],"tags":["python","pygments","formatter","filesystem","vendored"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}