New submission from Victor Varvariuc:

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.

----------
components: Interpreter Core
messages: 291376
nosy: Victor.Varvariuc
priority: normal
severity: normal
status: open
title: Treat `import a.b.c as m` as `m = sys.modules['a.b.c']`
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30024>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to