Skip to content

bpo-40108: Improve the error message in rumpy when importing a module including the extension #19239

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
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def _get_module_details(mod_name, error=ImportError):
# importlib, where the latter raises other errors for cases where
# pkgutil previously raised ImportError
msg = "Error while finding module specification for {!r} ({}: {})"
if mod_name.endswith(".py"):
msg += (f". Try using '{mod_name[:-3]}' instead of "
f"'{mod_name}' as the module name.")
raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex
if spec is None:
raise error("No module named %s" % mod_name)
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ def test_dash_m_bad_pyc(self):
self.assertNotIn(b'is a package', err)
self.assertNotIn(b'Traceback', err)

def test_hint_when_triying_to_import_a_py_file(self):
with support.temp_dir() as script_dir, \
support.change_cwd(path=script_dir):
# Create invalid *.pyc as empty file
with open('asyncio.py', 'wb'):
pass
err = self.check_dash_m_failure('asyncio.py')
self.assertIn(b"Try using 'asyncio' instead "
b"of 'asyncio.py' as the module name", err)

def test_dash_m_init_traceback(self):
# These were wrapped in an ImportError and tracebacks were
# suppressed; see Issue 14285
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve the error message when triying to import a module using :mod:`runpy`
and incorrently use the ".py" extension at the end of the module name. Patch
by Pablo Galindo.