简体   繁体   English

Python 最佳实践在子模块中导入子模块

[英]Python best practice import submodule in submodule

I'm currently reading about Python's submodule in submodule imports and somehow I can't find a proper answer.我目前正在阅读子模块导入中的 Python 子模块,但不知何故我找不到合适的答案。 Here is an example:这是一个例子:

root/
     main.py
     moduleA/
             __init__.py
             log.py
     moduleB/
             __init__.py
             worker.py

I'd like to import log in worker by using import moduleA.log .我想使用import moduleA.log导入log worker And I'd like to import worker in main and use it there.我想在main中导入worker并在那里使用它。

So far I've found the following solutions:到目前为止,我已经找到了以下解决方案:

  • Append sys.path with ../ ( sys.path.append('../') )sys.path附加到../ ( sys.path.append('../') )
  • I read something about using pip -e to install my module into the environment.我阅读了一些关于使用pip -e将我的模块安装到环境中的内容。
  • Avoiding scripts which import from submodules.避免从子模块导入的脚本。

I've read that the sys.path hack is considered the best practice.我读过sys.path hack 被认为是最佳实践。 But somehow it feels wrong.但不知何故感觉不对。

So I'd like to know what is considered best practice by you.所以我想知道您认为什么是最佳实践。

You can use relative imports to solve this issue:您可以使用相对导入来解决此问题:

In main.py writemain.py写入

from .moduleB import worker

In worker.py :worker.py

from ..moduleA import log

One dot takes the current directory as beginning of the path, so .moduleB digs into the folder moduleB below the current dir.一个点将当前目录作为路径的开头,因此.moduleB深入到当前目录下的文件夹moduleB中。
Two dots moves one dir upwards, so ..moduleA in goes up to the root directory and then down to moduleA .两个点向上移动一个目录,因此..moduleA in 向上移动到root目录,然后向下移动到moduleA Each additional dot is one directory up.每个额外的点是一个目录。
Fi from. import abfrom. import ab from. import ab would import ab.py from the same directory. from. import ab将从同一目录导入ab.py
Packaging/installing a module is not necessary to do relative imports.打包/安装模块不是进行相对导入所必需的。 But you have to import with from..moduleA import log .但是你必须使用from..moduleA import log Importing with import..moduleA.log won't work with relative imports.使用import..moduleA.log导入不适用于相对导入。

I'll typically use a modulename.py file that contains all the imports, which I can refer to later:我通常会使用包含所有导入的modulename.py文件,稍后我可以参考它:

With your structure:用你的结构:

root/
     __init__.py
     root.py
     modulea/
         __init__.py
         a.py
      moduleb/
         __init__.py
         b.py

The root.py would be: root.py将是:

from modulea.a import sleep, eat, breath
from moduleb.b import read, write, watch

such that I can later do这样我以后可以做

from package.root import sleep, read, watch

However, this requires having your package installed, which may not always be practical.但是,这需要安装您的软件包,这可能并不总是实用。

I personally prefer to change my current working directory and import just in case i ever run into this issue.我个人更喜欢更改我当前的工作目录并导入,以防我遇到这个问题。

root/
     __init__.py
     root.py
     modulea/
             __init__.py
             a.py
     moduleb/
             __init__.py
             b.py

Given a setup like this,给定这样的设置,

a.py
def a():
    print("i am a")

And in b, just change current directory to root using either absolute or relative path via os.chdir在 b 中,只需通过os.chdir使用绝对路径或相对路径将当前目录更改为根目录

b.py

def b():
    print("i am b")

import os
os.chdir('..')
from modulea.a import a
a()

Running b.py prints i am a运行 b.py 打印i am a

The other options are out there, sys.path or PYTHONPATH modifications.其他选项在那里,sys.path 或 PYTHONPATH 修改。 Refer here 参考这里

I do however also recommend avoiding such scenarios best you can as far as possible.然而,我也建议尽可能避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM