Skip to content

gh-159693: Format import errors with no usable search paths - #159830

Open
limadog9 wants to merge 1 commit into
python:mainfrom
limadog9:gh-159693-traceback-empty-path
Open

limadog9 wants to merge 1 commit into
python:mainfrom
limadog9:gh-159693-traceback-empty-path

Conversation

@limadog9

@limadog9 limadog9 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #159693.

When sys.path has no existing directories, the extension-module hint constructs an empty MultiplexedPath and raises FileNotFoundError while formatting the original ModuleNotFoundError.

This also affects the REPL: InteractiveColoredConsole.runcode() calls traceback.format_exception() through its exception hook. I reproduced the secondary exception through that console before the fix and verified that it displays the original import error afterward.

Return without a hint when there are no directories to search. The regression covers both an empty sys.path and a path containing only a nonexistent directory.

Validation on a Linux x86-64 debug build:

  • Both regression cases fail on unmodified main and pass with the fix.
  • ./python -m test -j2 test_traceback test_code test_pyrepl passed (833 test cases, 65 skips).
  • Ruff, NEWS reStructuredText lint, patchcheck, and git diff --check passed.

Comment thread Lib/traceback.py
*map(pathlib.Path, filter(os.path.isdir, sys.path))
)
paths = list(map(pathlib.Path, filter(os.path.isdir, sys.path)))
if not paths:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this check run before the importlib.resources import? If a fresh process clears sys.path, that import can fail before we get here.

@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

traceback formatting raises FileNotFoundError with empty sys.path

2 participants