diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -182,8 +182,8 @@ class ModuleFinder: if q: self.msgout(4, "find_head_package ->", (q, tail)) return q, tail - self.msgout(4, "raise ImportError: No module named", qname) - raise ImportError("No module named " + qname) + self.msgout(4, "raise ImportError: No module named %r" % qname) + raise ImportError("No module named %r" % qname) def load_tail(self, q, tail): self.msgin(4, "load_tail", q, tail) @@ -195,8 +195,8 @@ class ModuleFinder: mname = "%s.%s" % (m.__name__, head) m = self.import_module(head, mname, m) if not m: - self.msgout(4, "raise ImportError: No module named", mname) - raise ImportError("No module named " + mname) + self.msgout(4, "raise ImportError: No module named %r" % mname) + raise ImportError("No module named %r" % mname) self.msgout(4, "load_tail ->", m) return m @@ -212,7 +212,7 @@ class ModuleFinder: subname = "%s.%s" % (m.__name__, sub) submod = self.import_module(sub, subname, m) if not submod: - raise ImportError("No module named " + subname) + raise ImportError("No module named %r" % subname) def find_all_submodules(self, m): if not m.__path__: diff --git a/Lib/runpy.py b/Lib/runpy.py --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -101,19 +101,19 @@ def _get_filename(loader, mod_name): def _get_module_details(mod_name): loader = get_loader(mod_name) if loader is None: - raise ImportError("No module named %s" % mod_name) + raise ImportError("No module named %r" % mod_name) if loader.is_package(mod_name): if mod_name == "__main__" or mod_name.endswith(".__main__"): - raise ImportError("Cannot use package as __main__ module") + raise ImportError("Cannot use package as '__main__' module") try: pkg_main_name = mod_name + ".__main__" return _get_module_details(pkg_main_name) except ImportError as e: raise ImportError(("%s; %r is a package and cannot " + - "be directly executed") %(e, mod_name)) + "be directly executed") % (e, mod_name)) code = loader.get_code(mod_name) if code is None: - raise ImportError("No code object available for %s" % mod_name) + raise ImportError("No code object available for %r" % mod_name) filename = _get_filename(loader, mod_name) return mod_name, loader, code, filename diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -226,7 +226,7 @@ class CmdLineTest(unittest.TestCase): make_pkg(pkg_dir) main_dir = os.path.join(pkg_dir, '__main__') make_pkg(main_dir) - msg = ("Cannot use package as __main__ module; " + msg = ("Cannot use package as '__main__' module; " "'test_pkg' is a package and cannot " "be directly executed") launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -271,6 +271,9 @@ Core and Builtins Library ------- +- Issue #8754: Module names are now quoted (using repr) in all ImportError-like + messages emitted in the standard library. + - Issue #12636: IDLE reads the coding cookie when executing a Python script. - Issue #12494: On error, call(), check_call(), check_output() and