简体   繁体   English

运行此python代码时“没有名为...的模块”

[英]"no module named..." when running this python code

I have no idea what causes this error, this is as simplest as I could get, yet SO gives me hint that my question does not meet their quality standards so I must post this longer intro.我不知道是什么导致了这个错误,这是我能得到的最简单的方法,但 SO 提示我我的问题不符合他们的质量标准,所以我必须发布更长的介绍。

#===
# my_script.py
import myModule

#===
# myModule\__init__.py
import sys
from my_def import *

class MyModule:
  def __init__(self):
    pass

#===
# myModule\my_def.py
def my_def():
  pass

Traceback (most recent call last):
  File "my_script.py", line 1, in <module>
    import myModule
  File "D:\test\myModule\__init__.py", line 2, in <module>
    from my_def import *
ImportError: No module named 'my_def'

你可以试试这个:

sys.path.append("path_of_script")

In __init__.py , add a .__init__.py ,添加一个. before my_def .my_def之前。 The . . means that it should be looking for the file in the same folder as the script itself, rather than relative to my_script.py .意味着它应该在与脚本本身相同的文件夹中查找文件,而不是相对于my_script.py

from .my_def import *

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

相关问题 无法直接从 vscode 在 venv 中调试或执行 python 代码(没有命名模块...) - Can't debug or execute python code inside venv from vscode directly (No module named...) Python/Flask - 突然出现“ImportError: No module named...”错误 - Python/Flask - suddenly getting "ImportError: No module named..." error “No Module named...”-尽管代码似乎可以工作,但编辑器中仍出现错误 - "No Module named..."-error in editor despite code appearing to work 如何使用 VScode 在 python 中修复导入错误:No module named... - how to fix Import Error: No module named... in python with VScode Pytest“没有名为...的模块”问题 - Pytest "No module named..." issue 在 Django proyect 中没有名为...的模块 - No module named... in Django proyect 具有特定项目结构的“No module named...” - "No module named..." with certain project structure ModuleNotFoundError: No module named... Jupyter Notebooks - ModuleNotFoundError: No module named... Jupyter Notebooks pyinstaller - 'No module named...' 脚本导入错误 - pyinstaller - 'No module named...' error with scripts import Pytho.net 导入 class:“ModuleNotFoundError:没有名为...的模块” - Pythonnet importing class: "ModuleNotFoundError: No module named..."
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM