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.

classification
Title: `import..as` fails where `import` does not
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Aliasing import of sub-{module,package} from the package raises AttributeError on import.
View: 23203
Assigned To: Nosy List: brett.cannon, djmitche, eric.snow, martin.panter, r.david.murray
Priority: normal Keywords:

Created on 2015-06-29 14:49 by djmitche, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg245941 - (view) Author: Dustin J. Mitchell (djmitche) * Date: 2015-06-29 14:49
Reproduction:

# main.py
import foo.bar

# foo/__init__.py
# (empty)

# foo/bar/__init__.py
import foo.bar.bing as bing

# foo/bar/bing.py
# (empty)

Result:

dustin@euclid ~/tmp $ python3.3 main.py 
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import foo.bar
  File "/home/dustin/tmp/foo/bar/__init__.py", line 1, in <module>
    import foo.bar.bing as bing
AttributeError: 'module' object has no attribute 'bar'

dustin@euclid ~/tmp $ python2.7 main.py 
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import foo.bar
  File "/home/dustin/tmp/foo/bar/__init__.py", line 1, in <module>
    import foo.bar.bing as bing
AttributeError: 'module' object has no attribute 'bar'

If you remove the `as bing` from `foo/bar/__init__.py`, all is well.  Similarly, `from foo.bar import bing` works fine, as does `from foo.bar import bing as bing`.

I don't see anything in https://docs.python.org/3/reference/simple_stmts.html#import that suggests this is expected behavior.
msg245949 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-29 16:16
I'm pretty sure this has already been reported, but I have no idea how to search for the issue :)  If I remember correctly there's a doc patch, so I suppose it hasn't been applied.  But I could be misremembering.
msg245971 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-06-29 22:32
Maybe Issue 23203, proposing to fix the compiled bytecode.
msg245972 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-06-29 22:34
Ah, yes, that is the issue I was thinking of.  So, bug, not doc issue :)
History
Date User Action Args
2022-04-11 14:58:18adminsetgithub: 68718
2015-06-29 22:34:11r.david.murraysetmessages: + msg245972
2015-06-29 22:32:21martin.pantersetstatus: open -> closed

superseder: Aliasing import of sub-{module,package} from the package raises AttributeError on import.

nosy: + martin.panter
messages: + msg245971
resolution: duplicate
stage: resolved
2015-06-29 16:16:54r.david.murraysetnosy: + eric.snow, r.david.murray, brett.cannon
messages: + msg245949
2015-06-29 14:49:34djmitchecreate