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: from module import function creates package reference to the module
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, m.nijland, ncoghlan, r.david.murray
Priority: normal Keywords:

Created on 2016-07-17 18:14 by m.nijland, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg270658 - (view) Author: Marc (m.nijland) Date: 2016-07-17 18:14
Hello,

I've found an issue in python 2.7 and 3.4 and I don't if this is a bug or a feature that acts strange to me.

The import of a module or method from a module creates a reference in the package to that module only the first time, which could lead to unexpected behavior.

Description:
In following code there's one line marked with 'this line fixes the code to what I expected'
1. Without that line package.a.test() results in 'from module a'
2. With the line package.a.test() results in 'from module b'

Situation 1 is unexpected because I did not create the reference to 'module a', python did that with the statement 'from package.a import test' and this will happen from any place in the code that loads 'module a' for the first time. The documentation says that this reference will not be created. 

Kind regards,

Marc

# FILES USED
#
# test.py
#     package
# 	__init__.py
# 	a.py
# 	b.py
# ---------------------
# Content of a.py:


def test():
    print('from module a')

# ---------------------
# Content of a.py:


def test():
    print('from module b')

# ---------------------
# Content of __init__.py

#import a # <--- this line fixes the code to what I expected
import b as a
from a import test

# ---------------------
# Content of test.py

import package

print(dir(package))
package.a.test()
msg270668 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-07-17 23:52
I'm not at a computer where I can verify what you're seeing, Marc,but I should mention that at least for Python 3 your imports are wrong as you should be doing relative imports, e.g. `from . import b as a`, otherwise the imports won't work unless you're accidentally executing Python from within the directory of the package.
msg270671 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-18 00:30
The same example can be constructed for python3 by modifying the imports.

I think what is happening here is that if a has not yet been imported, then when 'from .a import test' is done, the import machinery loads a and then defines 'a' in the local namespace, overriding the previous 'import package.b as a'.  On the other hand, if a has already been imported, the import machinery finds 'a' in sys.modules and skips the import-and-set-name-in-local-namespace step.

IMO the bug is that 'a' gets set at all when 'from a import test' is done, but the fact that it does so has a long history.
msg270676 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2016-07-18 02:36
So the problem is that you doing `from .a import test` and not `from . import a; test = a.test`. Whenever you do `from X import Y`, import actually imports X. Since X wasn't in sys.modules, it performs a proper import which means it sets the attribute on the package accordingly. But had Y been what you were after, then import simply looks for the Y attribute on the package instead of doing a full import.

IOW everything is working as expected (and I still wish people weren't allowed to import objects out of modules directly).
msg270748 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-18 11:44
Note that conceptually this is closely related to issue 27515.  In both cases the conceptual problem (the thing that trips up user expectations) is that what import does to the namespaces differs depending on whether the base module exists in sys.modules or not.  I don't know that there is a fix, but it is certainly an inconsistency that causes mental hiccoughs.
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71730
2016-07-18 11:44:27r.david.murraysetmessages: + msg270748
2016-07-18 02:36:15brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg270676
2016-07-18 00:30:23r.david.murraysetnosy: + r.david.murray
messages: + msg270671
2016-07-17 23:52:01brett.cannonsetmessages: + msg270668
2016-07-17 18:28:59serhiy.storchakasetnosy: + brett.cannon, ncoghlan, eric.snow
2016-07-17 18:14:01m.nijlandcreate