13

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
  • 448
  • 1
  • 3
  • 12
  • 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

3 Answers3

7

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
  • 448
  • 1
  • 3
  • 12
  • 5
    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
4

In VSCode 1.74.0 I got it to work by putting the path in my debugging launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python debugging"
            "type": "python"
            // other settings
            "env": {
                "PYTHONPATH": "${workspaceFolder}",
            }
        }
    ]
}
1

Using venv I have tried every variation of tinkering with the terminal.integrated.env.x setting, and env/cwd in launch.json and while I could get this scenario OK when running a file, I could not get it working correctly when debugging a file.

So, what I ended up doing was modifying the .venv/bin/activate file locally to add the project to the python path as part of activation. I think this solution is fine as the venv is to be used only with this project and it covers all scenarios of running files within the IDE.

I added this to the bottom of myProject/.venv/bin/activate:

PYTHONPATH="/Users/path/to/your/project/:$PYTHONPATH"
export PYTHONPATH
jhnclvr
  • 9,137
  • 5
  • 50
  • 55