Skip to content

gh-127598: Improve ModuleNotFoundError when -S is passed #136821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

MonadChains
Copy link
Contributor

@MonadChains MonadChains commented Jul 19, 2025

This (partially) solves gh-127598 by adding flavour text to exception when the argument '-S' is passed.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Thanks, please add a test case and news entry.

Copy link
Contributor

@ilovelinux ilovelinux left a comment

Choose a reason for hiding this comment

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

Should we write a test for that?

Lib/traceback.py Outdated
elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \
sys.flags.no_site:
self._str += ". Site initialization is disabled, did you forget to add the \
site-package directory to sys.path?"
Copy link
Contributor

Choose a reason for hiding this comment

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

Traceback message has too many spaces in the middle:

ModuleNotFoundError: No module named 'foo'. Site initialization is disabled, did you forget to add the                 site-package directory to sys.path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be fixed now, please test it again

@MonadChains
Copy link
Contributor Author

@ZeroIntensity I've also add an additional check to exception as a suggestion from @encukou, I'll add also a test for it

@MonadChains
Copy link
Contributor Author

@ZeroIntensity I have pushed your feedbacks and I also add a test for it, let me know if something else is necessary.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, with two minor comments.

Comment on lines 4755 to 4761
cmd = [sys.executable, '-S', '-c', 'import boo']
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
Copy link
Member

Choose a reason for hiding this comment

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

We have a helper for doing this: test.support.assert_python_ok. Would you mind switching to that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok I've changed the to use it, although the version assert_python_failure as we looking for an exception

@@ -0,0 +1,2 @@
Improve :exc:`ModuleNotFoundError` by adding flavour text to exception when the
Copy link
Member

Choose a reason for hiding this comment

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

Oops, missed this the first time:

Suggested change
Improve :exc:`ModuleNotFoundError` by adding flavour text to exception when the
Improve :exc:`ModuleNotFoundError` by adding flavour text to the exception when the

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed

Lib/traceback.py Outdated
@@ -1106,6 +1106,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
if suggestion:
self._str += f". Did you mean: '{suggestion}'?"
elif exc_type and issubclass(exc_type, ModuleNotFoundError) and \
sys.flags.no_site and \
getattr(exc_value, "name", None) not in sys.builtin_module_names:
Copy link
Member

Choose a reason for hiding this comment

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

This feature should use stdlib_module_names, not builtin module names.

Suggested change
getattr(exc_value, "name", None) not in sys.builtin_module_names:
getattr(exc_value, "name", None) not in sys.stdlib_module_names:

(It could be tested by importing a non-existent stdlib module, like msvcrt on *nix or grp on Windows. The note should not be added for those -- you can't solve a "wrong platform" issue by adding the site-packages directory.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented the feedback, and it case stdlib the message it's not thrown :)

@encukou encukou added the sprint label Jul 20, 2025
)

code = """
import msvcrt
Copy link
Member

Choose a reason for hiding this comment

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

This test will fail on Windows, where msvcrt is available.
Could you either

  • go back to testing boo after adding it to stdlib_module_names, or
  • use grp on Windows, and msvcrt everywhere else?

Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

This version looks great! Thank you for the fix!

@encukou encukou merged commit 18a7f5d into python:main Jul 20, 2025
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants