Reputation: 468
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?
Upvotes: 14
Views: 23446
Reputation: 2150
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 ***
Upvotes: 0
Reputation: 9487
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
Upvotes: 1
Reputation: 126
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}",
}
}
]
}
Upvotes: 7
Reputation: 468
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.
Upvotes: 8