[Python-ideas] Make `import a.b.c as m` is equivalent to `m = sys.modules['a.b.c']`
victor.varvariuc at gmail.com
Sat Apr 8 01:54:50 EDT 2017
I asked a question <http://stackoverflow.com/questions/41845671/import-as-in-python-3> on Stackoverflow:
(Pdb) import brain.utils.mail
(Pdb) import brain.utils.mail as mail_utils
*** AttributeError: module 'brain.utils' has no attribute 'mail'
I always thought that import a.b.c as m is roughly equivalent to m = sys.modules['a.b.c']. Why AttributeError? Python 3.6
I was pointed out <http://stackoverflow.com/a/24968941/248296> that this is a somewhat weird behavior of Python:
The statement is not quite true, as evidenced by the corner case you met, namely if the required modules already exist in sys.modules but are yet uninitialized. The import ... as requires that the module foo.bar is injected in foo namespace as the attribute bar, in addition to being in sys.modules, whereas the from ... import ... as looks for foo.bar in sys.modules.
Why would `import a.b.c` work when `a.b.c` is not yet fully imported, but `import a.b.c as my_c` would not? I though it would be vice versa.
Using `import a.b.c as my_c` allows avoiding a number of circular import issues. Right now I have to use `from a.b import c as my_c` as a workaround, but this doesn't look right.
Does this make sense? Is there an issue in the Python bug tracker for this? Can/should I create one?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas