Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference an environment variable for python path in vscode launch configuration?

I am using vscode on my desktop and laptop and each machine would generate a random folder name with a hash for the virtual environment path when creating the virtual environment using poetry. In order to use the same launch.json file in my project for both computers, I'd like to reference an environment variable instead of hard coding the virtual environment path names. I've tried the below but vscode is stating "The Python path in your debug configuration is invalid." How can I reference the environment variable for the "python" path?

my ~/.zshrc:

export PROJ_VENV=$HOME/.cache/pypoetry/virtualenvs/myproj-NMmw6p6o-py3.12

my launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Django",
      "type": "python",
      "python": "${env:PROJ_VENV}/bin/python",
      "request": "launch",
      "program": "${workspaceFolder}/src/manage.py",
      "args": [
        "runserver",
      ],
      "django": true
    }
  ]
}
like image 961
bayman Avatar asked May 01 '26 15:05

bayman


1 Answers

As of vscode 1.85, specifying "python": "${env:PROJ_VENV}/bin/python" in launch.json will not work. As a workaround, you can remove "python" from launch.json and set "python.defaultInterpreterPath": "${env:PROJ_VENV}/bin/python" in settings.json. You can then select "Use Python from python.defaultInterpreterPath" when selecting your python interpreter.

like image 55
bayman Avatar answered May 03 '26 05:05

bayman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!