如何在多目录项目中正确导入python模块?

我有一个基本设置的 python 项目,如下所示:

imptest.py

utils/something.py
utils/other.py

以下是脚本中的内容:

测试文件

#!./venv/bin/python

import utils.something as something
import utils.other as other

def main():
    """
    Main function.
    """

    something.do_something()
    other.do_other()

if __name__ == "__main__":
    main()

东西.py

#import other

def do_something():
    print("I am doing something")


def main():
    """
    Main function
    """

    do_something()
    #other.do_other()

if __name__ == "__main__":
    main()

其他.py

def do_other():
    print("do other thing!")

def main():
    """
    Main function
    """

    do_other()

if __name__ == "__main__":
    main()

imptest.py 是偶尔运行和调用 utils 函数的主文件。

正如你所看到的,我在“something.py”中注释了一些我导入“other”模块进行测试的行。

但是当我想测试 something.py 中的某些功能时,我必须运行文件 something.py 并取消注释导入行。

这样做感觉有点笨拙。

如果我离开

import other

取消注释并运行imptest.py,我收到此错误:

Traceback (most recent call last):
  File "imptest.py", line 5, in <module>
    import utils.something as something
  File "...../projects/imptest/utils/something.py", line 3, in <module>
    import other
ModuleNotFoundError: No module named 'other'

这样做的更好方法是什么?

回答

这里的问题是路径,考虑这个目录结构

main
 - utils/something.py
 - utils/other.py
 imptest.py

当您尝试other使用相对路径导入to 时something.py,您会执行类似from . import other. 这在您执行时会起作用,$ python something.py但在您运行时会失败,$ python imptest.py因为在第二种情况下它会搜索不存在的 main/other.py。

所以为了解决这个问题,我建议你为 something.py 和 other.py 编写单元测试并使用$ python -m(mod) 命令运行它们。(我强烈推荐这种方法

但是....如果你真的你现有的代码不需要太多修改就可以工作,那么你可以在something.py文件中添加这两行(这有效,但我不推荐这种方法

import sys, os
sys.path.append(os.getcwd()) # Adding path to this module folder into sys path
import utils.other as other

def do_something():
    print("I am doing something")


def main():
    """
    Main function
    """

    do_something()
    other.do_other()

if __name__ == "__main__":
    main()

以下是一些参考资料,可以更好地理解:

  • python中的单元测试
  • python中的绝对与相对导入

以上是如何在多目录项目中正确导入python模块?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>