我只注意到相對進口是這樣的:的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