导入另一个模块的Python导入模块

时间:2019-05-03 21:17:15

标签: python python-3.x

我具有以下文档结构:

root
  |src
    |__init__.py
    |util.py
    |mymodule.py
  |__init__.py
  |test.py

init .py均为空

在util.py中,我具有一系列实用程序功能,例如a(),b(),c()。

在mymodule.py中,我有:

from util import a, b, c

def somefunc():
    # some stuff
    a()
    # more stuff

在test.py中,我有:

from src.mymodule import somefunc

我希望能够在src之外引用这些模块,但出现错误“从util import a,b,c,ModuleNotFoundError中出现mymodule.py中的错误:没有名为'util'的模块”

1 个答案:

答案 0 :(得分:0)

您需要使用模块路径(例如:from src.util import a, b, cfrom root.src.util import a, b, c)来导入utils,具体取决于您打算从何处运行/导入代码,后者用于从中运行/导入代码包含根目录的文件夹