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.

Author m.nijland
Recipients m.nijland
Date 2016-07-17.18:14:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1468779241.06.0.301525903069.issue27543@psf.upfronthosting.co.za>
In-reply-to
Content
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()
History
Date User Action Args
2016-07-17 18:14:01m.nijlandsetrecipients: + m.nijland
2016-07-17 18:14:01m.nijlandsetmessageid: <1468779241.06.0.301525903069.issue27543@psf.upfronthosting.co.za>
2016-07-17 18:14:01m.nijlandlinkissue27543 messages
2016-07-17 18:14:00m.nijlandcreate