简体   繁体   中英

ModuleNotFoundError when importing in python

I am a beginner in python and I have pulled code from a github repo to work on. But it is giving me the following error at the line from src.splendor import SplendorGame in test_splendor.py :

/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. Thanks in advance for any help!

There're two optional ways:

  1. Add the following code before import sentence in test_splendor.py :

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

    Then click the green button to run python file in Terminal.

  2. In launch.json , add the following configurations:

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

    This only works when you Run without debugging(Ctrl+F5) , click the green button will also throw the error.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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