Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 882
Bharel Avatar asked Nov 27 '25 06:11

Bharel


2 Answers

Came accross same issue today. It really looks like VSCode caches values somewhere, and pre-initializes the terminal with environment from cached (outdated .env). The workaround is to use override=True in load_dotenv(). Surely, looks like a bug.

like image 108
bleg Avatar answered Nov 30 '25 00:11

bleg


I arrived here because I don't want VSCode to load env vars automatically from my .env file.

The relevant VSCode Documentation says:

By default, the Python extension looks for and loads a file named .env in the current workspace folder, then applies those definitions.

The file is identified by the default entry "python.envFile": "${workspaceFolder}/.env" in your user settings (see General Python settings).

You can change the python.envFile setting at any time to use a different definitions file.

(I agree with Angel's comment - this seems like a dangerous default)

I will use the approach load_dotenv(override=True) from bleg's answer, AND set python.envFile to an empty string.

(Settings accessible in Code > Settings > Settings [Mac] or File > Settings > Settings [Windows/Linux])

like image 44
instreharne Avatar answered Nov 29 '25 23:11

instreharne



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!