This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Victor.Varvariuc
Recipients Victor.Varvariuc
Date 2017-04-09.11:11:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1491736314.56.0.10945117979.issue30024@psf.upfronthosting.co.za>
In-reply-to
Content
https://mail.python.org/pipermail/python-ideas/2017-April/045405.html

Hi there.

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.


The enhancement is to treat `import x.y.z as m` as `m = importlib.import_module('x.y.z')`. I don't see how this will break anything.
History
Date User Action Args
2017-04-09 11:11:54Victor.Varvariucsetrecipients: + Victor.Varvariuc
2017-04-09 11:11:54Victor.Varvariucsetmessageid: <1491736314.56.0.10945117979.issue30024@psf.upfronthosting.co.za>
2017-04-09 11:11:54Victor.Varvariuclinkissue30024 messages
2017-04-09 11:11:53Victor.Varvariuccreate