How to use PYTHONPATH with VSCode Python Extension for Debugging?

v-i-s-h picture v-i-s-h · Oct 26, 2019 · Viewed 10.5k times · Source

I'm working on a project which is structured like

Parent Directory
----+ MyPackage
     ----__init__.py
     ----file1.py
----+ Tests
     ----test.py

When I run the tests from terminal, I use

PYTHONATH=./ python ./Tests/test.py

Now, when I try the debug option after installing 'Python Extension', error is raised

Exception has occurred: ModuleNotFoundError
No module names 'MyPackage'

How can I put PYTHONPATH to the debug configuration such that it will taken care?

Answer

v-i-s-h picture v-i-s-h · Oct 28, 2019

After some search and trial and error, I found something that works. I'm posting it here so that people looking for the same problem can also try. I'm not sure whether this is the right way to do t.

Create (or add to) a file .vscode/settings.json the contents as

{
    // .. any other settings
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}"
      }
}

Now I'm able to run my project with the package.