15

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?

3
  • 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. Commented Oct 26, 2019 at 18:16
  • @rok No, that is wrong. Please read the official documentation regarding PYTHONPATH environment variable: docs.python.org/3/using/… Commented Apr 19, 2022 at 10:55
  • 1
    @niid. Thanks. Removed Commented Apr 19, 2022 at 16:38

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 which should work in all instances.
@Brett I'm not sure .env works any longer for python.pythontPath. See the note below #3: code.visualstudio.com/docs/python/…
8

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}",
            }
        }
    ]
}

Comments

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

Comments

0

For most people the best option is probably to just put

PYTHONPATH="${env:PYTHONPATH}:${workspaceFolder}"

into an .env file in the root of the workspace. This will add the workspace folder to PYTHONPATH without overriding it.

However, if you only want to the environment variable to be added to the integrated terminal, then you can add this to your settings

    "terminal.integrated.env.<<OS>>": {
        "PYTHONPATH": "${workspaceFolder}"
      }

where <<OS>> can be eg. linux, osx or windows

*** NB! Remeber to restart the integrated terminal after adding this ***

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.