visual-studio-codeenvironment-variables.envvscode-pythonpython-dotenv

VSCode does not update environment variables upon .env modification


I'm using .env in a Python project, with python-dotenv, but it seems like VSCode automatically loads the environment variables from the .env.

Unfortunately, VSCode does not seem to update the environment variables when I modify the .env file, causing the Python program to run on old environment vars.

Restarting VSCode / opening new integrated terminals does not seem to help. It looks as if it caches the .env somewhere, and reloads it from cache, without removing the cache when the original file is updated.

Output of /usr/bin/env together with .env file:

enter image description here

Output of Help: About:

Version: 1.84.0-insider (Universal)
Commit: f1c3b1dcf85e3b6ddb24b7dce0e4b122e8ce6233
Date: 2023-10-17T05:38:23.658Z
Electron: 25.8.4
ElectronBuildId: 24154031
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Darwin x64 22.6.0

Python extension version v2023.19.12931008.


Solution

  • Apparently it's a bug that arises from a faulty Microsoft A/B experiment that for some reason caches your .env.

    Essentially, if you have it, your specific VS Code Python extension installation was randomly chosen by Microsoft to stop working properly 🎉

    Opt out of Microsoft's experiment by adding the following line to your settings.json:

    "python.experiments.optOutFrom": ["All"]
    

    Thanks @Viacheslav for the solution.