Skip to content
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

Better traceback for bad imports in custom path #735

merged 1 commit into from Nov 22, 2018
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -186,6 +186,26 @@ def test_import_string():
pytest.raises(ImportError, utils.import_string, 'cgi.XXXXXXXXXX')

def test_import_string_provides_traceback(tmpdir, monkeypatch):
# Couple of packages
dir_a = tmpdir.mkdir('a')
dir_b = tmpdir.mkdir('b')
# Totally packages, I promise
# 'aa.a' that depends on 'bb.b', which in turn has a broken import
dir_a.join('').write('from b import bb')
dir_b.join('').write('from os import a_typo')

# Do we get all the useful information in the traceback?
with pytest.raises(ImportError) as baz_exc:
traceback = ''.join((str(line) for line in baz_exc.traceback))
assert '\':1' in traceback # a bit different than typical python tb
assert 'from os import a_typo' in traceback

def test_import_string_attribute_error(tmpdir, monkeypatch):
tmpdir.join('').write('from bar_test import value')
@@ -423,13 +423,7 @@ def import_string(import_name, silent=False):
return sys.modules[import_name]

module_name, obj_name = import_name.rsplit('.', 1)
module = __import__(module_name, None, None, [obj_name])
except ImportError:
# support importing modules not yet set up by the parent module
# (or package for that matter)
module = import_string(module_name)

module = __import__(module_name, globals(), locals(), [obj_name])
return getattr(module, obj_name)
except AttributeError as e:
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.