0

This question is 3 years old: Code is working in pyCharm but not in Visual Studio Code

So, maybe this is the reason why it doesn't work anymore. Anyway, I have this project structure:

enter image description here

It works perfectly when I run "main.py" under "01SpaceInvaders", but the same code in vscode returns this error:

Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "C:\Users\salva\Documents\VS Code\pysandbox\game\01SpaceInvaders\main.py", line 5, in <module>
    from game.sdfengine.locals import *
ModuleNotFoundError: No module named 'game'

For sure, there is a configuration missing in my VSCode, but I don't know what. Any clue?

This is how I execude the code from VSCode:

  1. I open a new terminal
  2. Go to 01SpaceInvaders
  3. py main.py

Following the comments it seems VSCode configuration problem:

Where can I find the file to edit and a guide to understand how to edit it?

rioV8
  • 24,506
  • 3
  • 32
  • 49
Salvatore Di Fazio
  • 705
  • 2
  • 7
  • 25
  • How exactly are you executing it? You just have the wrong working directory and/or environment path settings… – deceze Jul 31 '23 at 08:17
  • Well, you need to be in the directory `pysandbox` and run `game/01SpaceInvaders/main.py`, if you want `game` to be found. – deceze Jul 31 '23 at 08:37
  • 2
    alternatively you can add the `pysandbox` directory to your PYTHONPATH environment variable. I don't know the windows syntax to do that – Almog-at-Nailo Jul 31 '23 at 08:40
  • @Almog-at-Nailo so the problem is configuration as I thought. That means that does VSCode's project to be configured to works? Where can I find this file to edit and a guide? – Salvatore Di Fazio Jul 31 '23 at 08:44
  • You can try any of the solutions in this answer: https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages notice you would want to add the pysandbox path and not the python binary path to your PYTHONPATH environment variable – Almog-at-Nailo Jul 31 '23 at 09:07
  • Found a more official answer from python documentation: https://docs.python.org/3/using/windows.html#excursus-setting-environment-variables – Almog-at-Nailo Jul 31 '23 at 09:09

2 Answers2

2

The issue here is that python does not know where to find the directory called game since it is not an installed python library (it's a directory in your project structure)

To let python know of directories that it should consider as libraries to import from you can use the environment variable PYTHONPATH (see documentation here)

You can:

  1. configure your PYTHONPATH environment variable temporarily from command line:
set PYTHONPATH=%PYTHONPATH%;C:\Users\salva\Documents\VS Code\pysandbox
  1. configure it permanently on windows by searching for "edit environment variables" in your start menu

  2. edit your vscode settings.json file (see documentation here) ctrl + shift + p -> "open user settings (JSON)" and add:

"terminal.integrated.env.windows": {
    "PYTHONPATH": "${env:PYTHONPATH};C:\\your\\path\\here"
}
Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20
-2

The most straightforward way is to indicate the path by adding the following at the top of the code:

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

enter image description here

You can also configure PYTHONPATH using .env file

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • 3
    You should _not_ need to edit all your code files to futz with the path. You just need to execute your program using the right environment path settings. – deceze Jul 31 '23 at 08:16
  • Can VSCode not deal with this in a run configuration, like PyCharm can? That seems better than requiring users to pollute their source code. – slothrop Jul 31 '23 at 08:44