David Masip
David Masip

Reputation: 2491

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?

Upvotes: 2

Views: 2289

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8411

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.

Upvotes: 0

Benjamin Rio
Benjamin Rio

Reputation: 656

The play button vs launching from terminal

When you click on "the play button" on VSCode, it calls the current interpreter and puts the first parent directory of the script as the current working directory. Therefore, when the system won't find the module debug_example inside debug_example.

You should rather use the alternative method, I think it is a better habit for when working with complex projects and different environments.

Debugging with VSCode

To debug in VSCode, you simply have to add the following env entry to the launch.json configuration file (that you can open from the debug console):

{
    "version": "0.2.0",
    "configurations": [
        {
           "name": "Python: Current File",
           "type": "python",
           "request": "launch",
           "program": "${file}",
           "console": "integratedTerminal",
           "env": { "PYTHONPATH": "${workspaceRoot}"}
        }
    ]
}

Upvotes: 3

Related Questions