[issue13912] ImportError using __import__ and relative level 1

Jason R. Coombs report at bugs.python.org
Tue Jan 31 00:05:31 CET 2012


Jason R. Coombs <jaraco at jaraco.com> added the comment:

Thanks for the tip Brent. Still, no luck.

    jaraco at devjaraco:~$ python2.7 -c 'import master.pkgA; print("pkgA.__package__ is {}".format(master.pkgA.__package__)); import master.pkgA.foo'
    pkgA.__package__ is None
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "master/pkgA/foo.py", line 2, in <module>
        bar = __import__('pkgB', globals(), level=1).bar
    ImportError: No module named pkgB

I considered importlib, but it is only included for Python 2.7+ and I have to support Python 2.6. I'm using programmatic imports for a few  reasons:

1) I'm importing the modules for the side effects, so I don't need or want the name in the namespace.
2) If I import it naturally, I get pyflakes warnings that it's an used imports.
3) The order of imports is important. If I place it too early in the imports, it doesn't have the state it needs and fails.
4) By placing it in the top of the file with other imports, it's not obvious why it's being imported, so requires comments.
5) The most appropriate place to invoke this functionality is in a specific function, "run_app".

I see now that there is an importlib backport in the cheeseshop for earlier versions of Python. Given your recommendation, I'll give that a try.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13912>
_______________________________________


More information about the Python-bugs-list mailing list