'Cannot run python code in vs code from root folder

I have a folder debug_example in vs code with the following structure:

enter image description here

The content in main is:

from debug_example.src.util import my_util

if __name__ == '__main__':
    my_util()

whereas the content in util is:

def my_util():
    print("foo")

When I click on the "play" button in vs code

enter image description here

or debug it, or run it in any way from vs code, I get

ModuleNotFoundError: No module named 'debug_example'

But if I run it from the terminal it works fine:

python -m debug_example.main

When debugging this, I see that sys.path has the debug_example folder in it when running from vs code, but not when running from terminal. Is there any way for this to run in a regular way in vs code, as in terminal?



Solution 1:[1]

You can replace your main.py code with this code:

import sys
from pprint import pprint

pprint(sys.path)

When you click the Run Button, In fact, you are executing this command:

python debug_example\main.py

You can get it from the terminal outputs.

And through this, the parent folder path of main.py -> xxx\debug_example will be added to the PYTHONPATH.

When you execute python -m debug_example.main command, the parent folder path of debug_example will be added to the PYTHONPATH.

As you import through this:

from debug_example.src.util import my_util

The parent folder of debug_example needed to be in the PYTHONPATH. This is the reason why clicking Run Button does not work while executing the command in the terminal works, but if you execute the command python debug_example\main.py will not work too.

Solution:

The PYTHONPATH environment variable specifies additional locations where the Python interpreter should look for modules. In VS Code, PYTHONPATH can be set through the terminal settings (terminal.integrated.env.*) and/or within an .env file.

When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting will not have an effect on module look-up.

When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.

If needed, you can set PYTHONPATH using both methods.

official docs.

Add this in the settings.json file:

  "terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder};"
  },

Through this, both click Run Button and debugging will work.

And add this in the launch.json:

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

Or

create a .env file under the workspace folder then add this in it:

PYTHONPATH= { the path of workspace folder}

Both only work for debugging.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steven-MSFT