简体   繁体   English

将“兄弟”模块相互导入的最 Pythonic 的方法是什么?

[英]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.pyresource/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.

相关问题 在python中导入模块的最pythonic方法是什么 - What is the most pythonic way to import modules in python 我是否应该始终使用最pythonic的方式导入模块? - Should I always use the most pythonic way to import modules? 在 XML 中查找元素兄弟的最 Pythonic 方法 - Most Pythonic way to find the sibling of an element in XML 以最pythonic的方式将列表列表复制到另一个列表的一部分中 - Copying a list of lists into part of another one in the most pythonic way 用另一个反向扩展列表的最有效方法是什么? - What is the most pythonic way to extend a list with the reversal of another? map 两个不同的字符串作为一个字符串的最pythonic方法是什么? - What is the most pythonic way to map two different strings, to act as one? Pythonic从包中导入模块的方法 - Pythonic way to import modules from packages 对于具有指定编码的多个模块和多个处理程序,最具pythonic的日志记录方式是什么? - What is the most pythonic way of logging for multiple modules and multiple handlers with specified encoding? 让对象相互引用的pythonic方法是什么? - What is the pythonic way to have objects that reference one another? 将一个自定义类的序列映射到另一个自定义类的Python方法是什么? - What is the Pythonic way to map a sequence of one custom class to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM