vscode-python: launch.json environment variables not being passed to Python scripts

Environment data

  • VS Code version: 1.37.1
  • Extension version (available under the Extensions sidebar): 2019.8.30787
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7.13
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): virtualenv
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): Jedi Enabled = True

Expected behaviour

Environment variables configured in launch.json should always be set when a python script is run or debugged.

Actual behaviour

Environment variables configured in launch.json are sometimes set when a python script is run or debugged. Sometimes they are not.

Steps to reproduce:

In my launch.json I have:

"configurations": [
        {
            "name": "Python: current jobs",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/beany/beany/util/reporting/weeklywip.py",
            "console": "integratedTerminal",
            "env" : {
                "SANDBOX" : 1
            }    
        }
]

When debugging this, sometimes the command line includes the SANDBOX environment variable:

PS D:\workspace\vscode> cd 'd:\workspace\vscode'; ${env:SANDBOX}='1'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1'; & 'd:\workspace\vscode\beany\env\Scripts\python.exe' 'c:\Users\dunca\.vscode\extensions\ms-python.python-2019.8.29288\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '50043' 'D:\workspace\vscode/beany/beany/util/reporting/weeklywip.py'

…but sometimes it doesn’t, and the environment variable isn’t available to the script:

PS D:\workspace\vscode> & 'd:\workspace\vscode\beany\env\Scripts\python.exe' 'c:\Users\dunca\.vscode\extensions\ms-python.python-2019.8.29288\pythonFiles\ptvsd_launcher.py' '--default' '--client' '--host' 'localhost' '--port' '50035' 'D:\workspace\vscode/beany/beany/util/reporting/weeklywip.py'

If I edit and save weeklywip.py before running it seems more likely to include the environment variable as desired, but I haven’t figured out a clear pattern. This only started happening since Visual Studio Code updated to version 1.37.0 on 2019-08-08.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 42
  • Comments: 17

Most upvoted comments

This is a decent broken existing feature nearly a year later and still no fix ?

If this won’t be fixed, perhaps remove it from the documentation?

Major feature remains broken ?

Hi @kimadeline I’ve encountered this behaviour in VSCode 1.41.1. I can confirm your findings except one case. If one defines environment variable with value set to int, it is not available to the script at the first run:

For code:

import os

print(os.environ['TEST'])

below configuration fails at first run, works for subsequent

{
      "name": "Python: Current File 1",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "env": {
        "TEST": 1,
      }
    },

but this configuration works for every run

{
       "name": "Python: Current File 2",
      "type": "python",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "env": {
        "TEST": "1",
      }
    },