[英]What is the most pythonic way to import 'sibling' modules into one another?
By 'sibling' modules, I mean two submodules that exist at the same depth within a parent module.通过“兄弟”模块,我的意思是在父模块中存在于相同深度的两个子模块。
I'm trying to create a flask project using Flask-Restful, and it recommends structuring the project using this schema:我正在尝试使用 Flask-Restful 创建一个 Flask 项目,它建议使用以下模式构建项目:
myapi/
__init__.py
app.py # this file contains your app and routes
resources/
__init__.py
foo.py # contains logic for /Foo
bar.py # contains logic for /Bar
common/
__init__.py
util.py # just some common infrastructure
I really like this structure, but I'm not sure how to import something from the 'common' module into the 'resources' module.我真的很喜欢这种结构,但我不确定如何将某些东西从“common”模块导入到“resources”模块中。 Can anyone help me out?谁能帮我吗?
In common/__init__.py
common/__init__.py
from myapi.common.utils import A, B
In resource/foo.py
在resource/foo.py
from myapi.common import A
You can also relative imports in __init__.py
like from .utils import A
.您还可以在__init__.py
相对导入,例如from .utils import A
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.