Skip to content

traceback formatting raises FileNotFoundError with empty sys.path #159693

Description

@heikkitoivonen

Bug report

On CPython 3.15.0, formatting a ModuleNotFoundError raises a second exception when sys.path contains no existing directory.

import sys
import traceback
import importlib.resources.readers  # Load dependencies before clearing sys.path.

sys.path[:] = []

error = ModuleNotFoundError(
    "No module named 'example_missing'",
    name="example_missing",
)
print(traceback.format_exception_only(error))

Expected: Expected: A formatted ModuleNotFoundError. (["ModuleNotFoundError: No module named 'example_missing'\n"])

Actual: exception raised (FileNotFoundError: MultiplexedPath must contain at least one path)

The same condition formats successfully on CPython 3.14.8. A path containing only nonexistent directories also triggers the failure; adding an existing directory prevents it.

In _find_incompatible_extension_module() (https://github.com/python/cpython/blob/v3.15.0/Lib/traceback.py#L2057), filtering sys.path can leave no arguments for MultiplexedPath, whose exception escapes the formatter.

Found on Linux ARM64 using a uv-managed CPython 3.15.0 build, and reproduced on macOS

  • Linux: Python 3.15.0 (main, Oct 14 2026, 01:07:17) [Clang 23.1.3 ]
  • macOS: Python 3.15.0 (main, Oct 9 2026, 15:11:47) [Clang 22.1.3 ]

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 19, 2026
  2. picnixz commented on Oct 19, 2026

    @picnixz
    Member

    I would personally think it's weird to have no sys.path at all but the fix should be fairly simple so it's fine to fix this as well. Did you encounter this in a legitimate session btw?

  3. heikkitoivonen commented on Oct 19, 2026

    @heikkitoivonen
    ContributorAuthor

    I was adding support for 3.15.0 on my https://pythoncomplexity.com/ site, and Claude/Codex found this when working on unit tests.

  4. picnixz commented on Oct 19, 2026

    @picnixz
    Member

    Yeah we don't wan't to do defensive programming actually. I honestly can't imagine a situation where you would empty your path for a long time. However, what I can expect is:

    • Empty sys.path for some reasons
    • Do something to fill it with some other stuff
    • Then hit an exception.
    • And the traceback formatting fails for some reason.

    I don't know if format_exception_only is called and if it raises, then is ignored in the interpreter somewhere and rewritten in C only just in case, but if we use format_exception_only implicitly when raising exceptions, then I would accept a fix. If we don't use traceback for formatting exceptions in the REPL or elsewhere, I would rather reject the fix.

    General rule: please don't report issues found by LLMs if they are not realistic in general (hence my question about a real situatio happening).

  5. added
    pendingThe issue will be closed if no feedback is provided
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions