Python无法从包中导入模块 [英] Python can't import module from package
问题描述
我有一个Flask Restful项目,其布局如下(为方便起见,更改了文件名)
I have a flask restful project with the following layout (file names changed for convenience)
myproject/
__init__.py
app.py
common/
__init__.py
util.py
foo/
__init__.py
main.py
utilities.py
foo/
只是一个包含API端点之一的代码的文件夹,我计划将来添加其他端点,因此,我有common/util.py
文件,其中包含将与其他API一起使用的可重用函数.端点.
foo/
is just a folder containing code for one of the API endpoints, I'm planning to add others in the future, for this reason I have common/util.py
file which contains reusable functions that I will use with other API endpoints.
foo/main.py
foo/main.py
from flask_restful import Resource, request
from utilities import Analysis
class Foo(Resource):
def get(self):
pass
foo/utilities.py
中,我的类带有一些获取数据的方法,我将这些类导入到foo/main.py
中以返回JSON响应
in foo/utilities.py
I have classes with methods that get some data, I import those classes to foo/main.py
to return JSON response
类还使用了common/util.py
中的某些函数,但是当我尝试将某些内容从common/util.py
导入到foo/utilities.py
时,我得到了import common.util
ModuleNotFoundError: No module named 'common'
classes in foo/utilities.py
also uses some functions from common/util.py
but when I try to import something from common/util.py
to foo/utilities.py
I get import common.util
ModuleNotFoundError: No module named 'common'
可能是什么原因造成的?我尝试导入各种方式:
from common.util import my_func
from .common.util import my_func
from myproject.common.util import my_func
What could be causing this? I tried importing various ways:
from common.util import my_func
from .common.util import my_func
from myproject.common.util import my_func
但无济于事.
这很重要,它是myproject/app.py
:
from flask import Flask
from flask_restful import Api
from foo.main import Foo
app = Flask(__name__)
api = Api(app)
api.add_resource(Foo, '/Foo')
if __name__ == "__main__":
app.run()
如果需要的话,我会在激活的virtualenv中进行所有操作
I'm doing all of this in activated virtualenv if it matters
推荐答案
从common.util导入my_func
from common.util import my_func
在Python 3中,这是绝对导入,也就是说,带有common/
子目录的目录必须位于sys.path
中.在您的情况下,这肯定是错误的方法.
In Python 3 this is an absolute import, that is, the directory with common/
subdirectory must be in sys.path
. In your situation it's certainly a wrong approach.
从.common.util导入my_func
from .common.util import my_func
此导入期望common
是foo
的子目录,但情况并非如此.
This import expects common
to be a subdirectory of foo
which is also not the case.
从myproject.common.util导入my_func
from myproject.common.util import my_func
这最终是最好的方法,但要使其正常工作,myproject/
子目录的父目录必须位于sys.path
中.您可以安装整个myproject
或将父目录添加到$PYTHONPATH
环境变量,或者将目录添加到foo/main.py
中的sys.path
.像这样:
This is finally the best approach but for it to work the parent directory of myproject/
subdirectory must be in sys.path
. Either you install the entire myproject
or add the parent directory to $PYTHONPATH
environment variable or add the directory to sys.path
in foo/main.py
. Something like:
PYTHONPATH=/home/to/parentdir /home/to/parentdir/myproject/foo/main.py
或
import sys
sys.path.insert(0, '/home/to/parentdir')
/home/to/parentdir
是myproject/
所在的目录.
在安装myproject
或将其父目录添加到sys.path
之后,您也可以使用相对导入.您需要记住,与foo
相比,common
是同级软件包,因此导入必须不是从.common
而是从..common
:
After installing myproject
or adding its parent directory to sys.path
you can also use relative import. You need to remember that common
is a sibling package comparing to foo
so the import must be not from .common
but from ..common
:
from ..common.util import my_func
这篇关于Python无法从包中导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!