[New-bugs-announce] [issue31385] `import as` does not work when module has same same as parent module

David Hagen report at bugs.python.org
Thu Sep 7 15:11:59 EDT 2017


New submission from David Hagen:

Consider the following Python project:

bugtest/
  __init__.py (Contents: from .foo import *)
  foo/
    __init__.py (Contents: from .foo import *)
    foo.py (Contents: <empty file>)

Then in a Python session, the following line executes without error (as expected):

>>> import bugtest.foo.foo

However, the following line gives an error (not as expected):

>>> import bugtest.foo.foo as bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'bugtest.foo.foo' has no attribute 'foo'

Note that this behavior is dependent on the folder foo and the file foo.py having the same base name. But is not dependent on actually trying to import bugtest.foo.foo. Trying to import bugtest.foo.baz will also fail as long as bugtest.foo.foo exists.

It is also dependent on the __init__.py files importing something from their respective submodules.

----------
messages: 301614
nosy: David Hagen
priority: normal
severity: normal
status: open
title: `import as` does not work when module has same same as parent module
type: behavior
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31385>
_______________________________________


More information about the New-bugs-announce mailing list