【问题标题】:ModuleNotFoundError: No module named <module-name>ModuleNotFoundError:没有名为 <module-name> 的模块
【发布时间】:2022-01-01 03:27:23
【问题描述】:

我的项目目录中有一个名为 ImgUtils 的包,如下所示:

├── myproject
│   ├── test.py
│   ├── Analyzer
│      ├── __init__.py
│      ├── utils.py
│      └── ImgUtils
│          ├── __init__.py
│          ├── ImgUtils.pyd
│          ├── ImgUtils.so
│          └── a bunch of *.dll files

ImgUtils.pyd 导出一个名为 PyImageUtils 的类。

\\ utils.py
from ImgUtils.ImgUtils import PyImageUtils

def my_test_function():
    a = PyImageUtils.analyze()
    return a
\\ test.py
from Analyzer import utils

print(utils.my_test_function())

utils.py 中的导入工作正常,但如果我运行 test.py,我会得到 ModuleNotFoundError: No module named 'PyImgUtils'

我正在使用 Python 3.10。 ImgUtils.pyd 的路径在 sys.path 列表中。

【问题讨论】:

  • 你启动 jupyter notebook 的根目录与你直接执行 python 脚本的根目录是什么?
  • @matszwecja 两种情况下的根目录相同。但是 jupyter notebook 在 conda 环境中运行,而脚本却没有。

标签: python-3.x django package python-import python-packaging


【解决方案1】:

更新:我可以通过像这样导入 ImgUtils 来解决这个问题:

\\ utils.py

from myproject.Analyzer.ImgUtils.ImgUtils import PyImageUtils

我使用的是 Django,this 回答解释了解决方案。

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 2022-01-07
    • 2017-12-14
    • 2020-12-10
    • 2021-09-02
    • 2020-10-10
    • 2022-01-06
    • 2021-09-10
    相关资源
    最近更新 更多