From what I found in the community, there are two options to extend the Python search path with further libraries.
.env
e.g.PYTHONPATH="C:\path\to\a;C:\path\to\b"
launch.json
e.g.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}/lib"
}
}
]
}
After searching again for this topic on multiple days, I did not yet find any clear solution for that. Some things work as expected, others don't. To be clear, it might be just a wrong understanding of the concepts. Since I found multipe threads on the topic, but almost just the same answers, which did not yet work out for me. There are multiple threads aroung that stuff, so I assume some general interest for a clarification.
Currently I tested exlusively each option. Using both things in parallel is somehow not comfortable,
also since the capabilities are different. .env
does not support pre-defined variables,
lauch.json
, does. So I tested .env
without settings in launch.json
and no .env
file + settings in launch.json
.
I use VS Code 1.75.1, but it was the case also for multiple previous version. Anaconda Python3 with different virtual environments.
What I see is the following, when using the options in VS Code:
yes --> extending the PYHTONPATH worked as expected no --> extending the PYHTONPATH dir NOT worked as expected
Run option | .env setting | launch.json setting |
---|---|---|
Run Python file in terminal | no | no |
Run in interactive Window | yes | no |
Debug via F5 | yes | yes |
"Debug Python File" (*) | yes | no |
(*) Debug via "Debug Python File" in Drop-Down of Run Symbol above right of the editor window
I assumed both debug options should be the same, but I have sometimes situations, where starting Debug via F5 works as expected, but using the Debug Symbol reports an error.
Not clear, why both debug options behave differently. No idea, why running Python terminal, does ignore path settings completely. In general more detailed information about the ideas behind both optios, would be very helpful. Why there is a difference for just running code in the terminal and debugging. The concept of launch configurations I know from other IDEs and it makes a lot of sense to me. Is there a way define such conigurations hiarchically, so only changes have to be added for lower levels and the rest can be kept common.
Setting all environment stuff on OS/command shell level, is possible via system/user variables. But I thought, that VS Code is doing that specifically for each workspace/project.
What am I doing wrong? What do have I missed during setup of VS Code? Or where do I not yet understand the concecept/workflow correctly.
I expected to control the library path settings for each workspace seperately. Potenitally in just one place (one configuration setting) in the workspace area (.env or launch.jason, or what ever other way)
Any help/comment is appreciated, thanks a lot,
Mike