[New-bugs-announce] [issue27543] from module import function creates package reference to the module

Marc report at bugs.python.org
Sun Jul 17 14:14:01 EDT 2016


New submission from Marc:

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()

----------
components: Interpreter Core
messages: 270658
nosy: m.nijland
priority: normal
severity: normal
status: open
title: from module import function creates package reference to the module
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27543>
_______________________________________


More information about the New-bugs-announce mailing list