Skip to content

Conversation

TimFelixBeyer
Copy link

Improve error message when given incorrect module path

Currently, python returns a helpful error message when users accidentally try to run python -m and include a '.py' ending in the path.

user@machine:~$ python3 -m path.to.file.py
>>> Error while finding module specification for 'path.to.file.py' 
(ModuleNotFoundError: No module named 'path'). 
Try using 'path.to.file' instead of 'path.to.file.py' as the module name.

However, a more common scenario for inexperienced users would be using the path to the file directly:

user@machine:~$ python3 -m path/to/file.py
>>> Error while finding module specification for 'path/to/file.py' 
(ModuleNotFoundError: No module named 'path/to/file'). 
Try using 'path/to/file' instead of 'path/to/file.py' as the module name.

This PR changes the error message to the more helpful:

user@machine:~$ python3 -m path/to/file.py
>>> Error while finding module specification for 'path/to/file.py' 
(ModuleNotFoundError: No module named 'path/to/file'). 
Try using 'path.to.file' instead of 'path/to/file.py' as the module name.

@python-cla-bot
Copy link

python-cla-bot bot commented Oct 13, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Oct 13, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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.

1 participant