简体   繁体   English

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

[英]Python importing module that imports another module

I have the following document structure: 我具有以下文档结构:

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

Both init .py are empty 两个init .py都是空的

In util.py I have a series of utility functions, for example a(), b(), c(). 在util.py中,我具有一系列实用程序函数,例如a(),b(),c()。

In mymodule.py I have: 在mymodule.py中,我有:

from util import a, b, c

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

In test.py I have: 在test.py中,我有:

from src.mymodule import somefunc

I would like to be able to reference these modules outside of src, but I am getting the error "error in mymodule.py from util import a, b, c, ModuleNotFoundError: No module named 'util'" 我希望能够在src之外引用这些模块,但出现错误“从util import a,b,c,ModuleNotFoundError中出现mymodule.py错误:没有名为'util'的模块”

您需要使用模块路径来导入utils,例如: from src.util import a, b, cfrom root.src.util import a, b, c这取决于您计划在何处运行/导入代码。如果您从包含root的文件夹中运行/导入代码

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

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