Author Victor.Varvariuc
Recipients Victor.Varvariuc
Date 2017-04-09.11:11:53
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>

Hi there.

I asked a question <> 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 <> 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 is injected in foo namespace as the attribute bar, in addition to being in sys.modules, whereas the from ... import ... as looks for 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.
Date User Action Args
2017-04-09 11:11:54Victor.Varvariucsetrecipients: + Victor.Varvariuc
2017-04-09 11:11:54Victor.Varvariucsetmessageid: <>
2017-04-09 11:11:54Victor.Varvariuclinkissue30024 messages
2017-04-09 11:11:53Victor.Varvariuccreate