5

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?

v-i-s-h
  • 338
  • 1
  • 2
  • 10
  • 1
    On Windows, the environment variable 'path' should point to your Python installation, meaning the interpreter. If VS Code is using the environment variable 'pythonpath', you should be able to add the path to your own module so that VS Code knows where to look for stuff to import. – FObersteiner Oct 26 '19 at 18:16
  • @rok No, that is wrong. Please read the official documentation regarding PYTHONPATH environment variable: https://docs.python.org/3/using/cmdline.html?highlight=pythonpath#envvar-PYTHONPATH – niid Apr 19 '22 at 10:55
  • 1
    @niid. Thanks. Removed – rok Apr 19 '22 at 16:38

1 Answers1

4

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.

v-i-s-h
  • 338
  • 1
  • 2
  • 10
  • 4
    That solution will only work when you do something that runs through the terminal (e.g. debugging), but it won't work for things when the interpreter does it on your behalf (e.g. runs tests using the Test Explorer). You can also [define an environments definition file](https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file) which should work in all instances. – Brett Cannon Oct 28 '19 at 21:41
  • 1
    @Brett I'm not sure .env works any longer for python.pythontPath. See the note below #3: https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter – Natetronn Sep 09 '20 at 03:44