我的文件结构是这样的:
app/
__init__.py
a.py
在__init__.py中定义函数
def fun1():
print('hehe')
在a.py中import,下边的都失败了
from . import fun1
from ..app import fun1
成功的方式有一点粗暴
from .__init__ import fun1
不过没见过这样import的,在网上搜的都不管用,不知道怎么回事。。。。谢谢啦
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
__init__.py的作用是为你写的package做初始化工作,通常也可以为空,一般不做具体实现。
所以最佳的做法是把__init__.py中的业务抽出来放在其他文件。
对于你的使用方法,在模块内部,也就只能使用
成功import了,不建议这样做,最好还是把代码移除来。
[1] http://stackoverflow.com/ques...
a.py