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: relative import unexpectedly binds name
Type: Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Antony.Lee, benjamin.peterson, brett.cannon, eric.snow
Priority: normal Keywords:

Created on 2012-08-09 00:18 by Antony.Lee, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg167742 - (view) Author: Antony Lee (Antony.Lee) * Date: 2012-08-09 00:17
The language reference is clear:
"The from form does not bind the module name" (Section 6.12)

However, consider the following example:

* package/__init__.py:
foo = "FOO"
from .foo import bar
print(foo)
os = "OS"
from os import path
print(os)

* package/foo.py:
foo = 42

Now "import package" results in printing:
<module 'pypackage.foo' from 'pypackage/foo.py'>
OS

i.e., the relative "from ... import" has not only bound "bar", but also "foo", which seems to contradict the documentation.
(I get the same behaviour using 3.2.3 as well as 2.7.3.)
msg167746 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-08-09 00:41
I don't think this is incorrect. Importing a submodule implicit sets its name in the package scope, which is __init__'s module scope.
msg167752 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2012-08-09 01:44
I agree with Benjamin.  The name is not bound due to the from-import statement.
History
Date User Action Args
2022-04-11 14:57:34adminsetgithub: 59803
2012-08-09 01:47:35benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2012-08-09 01:44:48eric.snowsetnosy: + eric.snow
messages: + msg167752
2012-08-09 00:41:36benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg167746
2012-08-09 00:18:36vstinnersetnosy: + brett.cannon
2012-08-09 00:18:07Antony.Leecreate