set the PYTHONPATH when debugging with F5 in Visual Studio Code (VS Code)#
Option 1: Use env in launch.json#
When you press F5, VS Code uses the configuration in .vscode/launch.json.
You can set PYTHONPATH (and other environment variables) directly there:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Debug App",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceFolder}/src"
}
}
]
}
π Replace ${workspaceFolder}/src with the path you want added to the Python module search path.
When you press F5, VS Code will:
Set
PYTHONPATHbefore launching the debugger.Your app will see the correct import paths.
Option 2: Use .env file#
You can also define environment variables (including PYTHONPATH) in a .env file, and tell VS Code to use it.
Create a file called
.envin your project root:PYTHONPATH=src
In
launch.json, reference it:{ "name": "Python: Debug App", "type": "python", "request": "launch", "program": "${file}", "envFile": "${workspaceFolder}/.env" }
Then pressing F5 will load PYTHONPATH from .env before debugging starts.
Option 3: Add path dynamically in code (less preferred)#
If you canβt modify launch.json, you can add the path programmatically:
import sys
sys.path.append("/path/to/your/src")
β¦but this is usually not ideal β better to configure it in VS Code.