我有以下的情況,其中一個是目錄:相對進口不起作用,貌似找不到模塊
a/
__init__.py
first.py
second.py
__init__.py
print('i\'m starting the directory')
__all__ = ['second', 'first']
first.py
print('hi, i\'m the first')
from . import *
second.py
print('hi, i\'m the second')
所以,當我從交互提示符下運行:
>>> import a.first
i'm starting the directory
hi, i'm the first
hi, i'm the second
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/antox/Scrivania/a/first.py", line 2, in <module>
from . import *
AttributeError: 'module' object has no attribute 'first'
爲什麼它不找到first.py模塊?我的意思是我希望沒有錯誤;在進口運行期間,我認爲它可以看到first.py已經加載,所以沒有錯誤,它只是跳到下一個列出的__all__
。
http://en.wikipedia.org/wiki/Circular_dependency – Vor 2015-02-11 16:11:15
將'import first,second'加入'__init __。py',並從'first.py'中刪除'import'。 – jonrsharpe 2015-02-11 16:14:50
是的,我知道還有其他方法可以獲得相同的好處,但我想知道這種情況下發生了什麼 – zer0uno 2015-02-11 16:17:40