Skip to content

Commit b784521

Browse files
author
Sergey Skripnick
committed
Show more verbose message on import errors
ImportError is not always 'module not found', so we should display more verbose message when this exception occured. For example message: aiohttp.web: error: unable to import defiler.server: No module named 'aiomysql' is more useful then: aiohttp.web: error: module 'defiler.server' not found
1 parent de17c89 commit b784521

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

aiohttp/web.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,8 @@ def main(argv):
366366
arg_parser.error("relative module names not supported")
367367
try:
368368
module = import_module(mod_str)
369-
except ImportError:
370-
arg_parser.error("module %r not found" % mod_str)
369+
except ImportError as ex:
370+
arg_parser.error("unable to import %s: %s" % (mod_str, ex))
371371
try:
372372
func = getattr(module, func_str)
373373
except AttributeError:

tests/test_web_cli.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ def test_entry_func_relative_module(mocker):
6969
def test_entry_func_non_existent_module(mocker):
7070
argv = ["alpha.beta:func"]
7171

72-
mocker.patch("aiohttp.web.import_module", side_effect=ImportError)
72+
mocker.patch("aiohttp.web.import_module",
73+
side_effect=ImportError("Test Error"))
7374
error = mocker.patch("aiohttp.web.ArgumentParser.error",
7475
side_effect=SystemExit)
7576

7677
with pytest.raises(SystemExit):
7778
web.main(argv)
7879

79-
error.assert_called_with("module %r not found" % "alpha.beta")
80+
error.assert_called_with('unable to import alpha.beta: Test Error')
8081

8182

8283
def test_entry_func_non_existent_attribute(mocker):

0 commit comments

Comments
 (0)