【发布时间】:2015-04-12 02:17:12
【问题描述】:
我有以下情况,其中a是一个目录:
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__ 中列出的下一个。
【问题讨论】:
-
将
import first, second添加到__init__.py,并从first.py中删除import。 -
是的,我知道还有其他方法可以获得相同的好处,但我想了解在这种情况下发生了什么
-
发生了什么是
__all__要求__init__.py公开first,但它不知道那是什么。
标签: python python-3.x