简体   繁体   English

在 python 中导入时出现 ModuleNotFoundError

[英]ModuleNotFoundError when importing in python

I am a beginner in python and I have pulled code from a github repo to work on.我是 python 的初学者,我已经从 github repo 中提取了代码来继续工作。 But it is giving me the following error at the line from src.splendor import SplendorGame in test_splendor.py :但它在test_splendor.py中的from src.splendor import SplendorGame行给了我以下错误:

/usr/bin/python3 "/Users/b.w.h/Documents/JHU/Google Game Theory Development/Splendor-AI/test/test_splendor.py"
Traceback (most recent call last):
  File "/Users/b.w.h/Documents/JHU/Google Game Theory Development/Splendor-AI/test/test_splendor.py", line 16, in <module>
    from src.splendor import SplendorGame
ModuleNotFoundError: No module named 'src'

And the file structure looks as follows: file structure文件结构如下:文件结构

I know it has something to do with the python interpreter path but I am not sure how to fix it.我知道它与 python 解释器路径有关,但我不确定如何修复它。 Thanks in advance for any help!提前感谢您的帮助!

There're two optional ways:有两种可选方式:

  1. Add the following code before import sentence in test_splendor.py :test_splendor.py中的 import 语句之前添加以下代码:

     import sys sys.path.append("./")

    Then click the green button to run python file in Terminal.然后单击绿色按钮在终端中运行 python 文件。

  2. In launch.json , add the following configurations:launch.json中添加如下配置:

     "env": { "PYTHONPATH":"${workspaceFolder}" }, "cwd": "${workspaceFolder}"

    This only works when you Run without debugging(Ctrl+F5) , click the green button will also throw the error.这仅在您运行而不调试时有效(Ctrl+F5) ,单击绿色按钮也会引发错误。

在此处输入图像描述

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

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