我只注意到相对进口是这样的:的Python:相对进口进口全包
from .foo import myfunc
print myfunc # ok
print foo # ok
进口都foo和MYFUNC。这种行为是否记录在任何地方我可以禁用它吗?
- 更新
基本上问题在于。
bar/foo/__init__.py
:
__all__ = ['myfunc']
def myfunc(): pass
bar/__init__.py
:
from .foo import *
# here I expect that there is only myfunc defined
main.py
:
import foo
from bar import * # this import shadows original foo
我可以添加__all__
到bar/__init__.py
为好,但这种方式,我要重复的名字在几个地方。
请提供关于你的软件包布局以及这些语句出现的文件的信息。(我猜这些代码行在'__init __。py'中。) – 2012-07-20 14:51:20
@SvenMarnach,yes,__init__.py。我会更新问题。 – 2012-07-20 14:53:01
这是避免通配符导入的另一个原因。 – jfs 2012-07-20 15:13:13