VSCode does not update environment variables upon .env modification
Asked Answered
W

5

13

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.

Wordage answered 22/10, 2023 at 6:56 Comment(4)
Please copy and paste the output of the Help: About command (run in the command palette) into your question post. Also add the version number of your Python extension. Then add an exact procedure that can be used to reproduce the problem.Olindaolinde
@Olindaolinde I've checked, thanks. Unfortunately, all of the issues are closed, and I'm running the latest build. I've seen them before, but this is a little mind boggling. It seems like the env file is being re-read when I restart vscode, use the developer reload-window, and open a new integrated terminal. If I miss any step it does not reload. It's rather difficult to add an exact procedure that reproduces it. It's simply a dotenv not being loaded to the environment (only after restarts etc.)Wordage
I'd suggest to raise a bug ticket using Help > Report Issue in the menu bar. If you do, please ping me here with a link to your issue ticket and put as much detail as you can into your issue ticket.Olindaolinde
Try >Show Environment Contributions. Then you can see your problematic env vars and what modifies them. In my case it was ms-python.python. Turned out it had some experiments messing with ENV. I disabled experiments in settings.json with "python.experiments.optOutFrom": ["All"] and this was it.Weiser
W
0

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.

Wordage answered 1/8, 2024 at 18:15 Comment(0)
O
15

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.

Ophiology answered 1/2, 2024 at 12:27 Comment(2)
VSCode should not do this kind of operation it could have very bad outcomes. For example, you are changing environment variables from production to testing database... you could perform operations that you may regret in the futureSgraffito
Been dealing with this for months when starting a django dev server, and thought it was user error. Running python3 manage.py runserver in my terminal would correctly load .env.dev as specified, but running from VSCode would just load the default .env file. Found this answer today when digging back in, and passing in override=True solved it for me. Thank you!Enalda
C
3

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])

Cobnut answered 22/5, 2024 at 13:12 Comment(1)
this doesn't seem to be what the question is asking about?Olindaolinde
W
0

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.

Wordage answered 1/8, 2024 at 18:15 Comment(0)
H
-1

It might be helpful for the developers who are using pydantic_settings "SettingsConfigDict" to load env files.

All we need to remove the entry in VSCode (Version: 1.90.1 (Universal)) > Settings > User > Extensions > Python - Text Box of "Env File" Should be blank

enter image description here

Hemistich answered 21/6, 2024 at 12:53 Comment(0)
P
-2

I'm not sure how your Python code is getting environment variables, but this is how I usually do.

.env

BROADCAST_MAC=ff:ff:ff:ff:ff:ff
DEFAULT_MAC=11:22:33:44:55:66

config.py

from dotenv import load_dotenv
from pathlib import Path
import os

# path to your .env file
env_path = Path(".") / ".env"
load_dotenv(dotenv_path=env_path)

class Config: # Save your var in a class
    BROADCAST_MAC = os.getenv("BROADCAST_MAC")
    DEFAULT_MAC = os.getenv("DEFAULT_MAC")

main.py

import config

BROADCAST_MAC = config.Config.BROADCAST_MAC
DEFAULT_MAC = config.Config.DEFAULT_MAC

I personally like separated file for configurations (easier to debug). Let me know if this helped, if not, this might help

Powe answered 22/10, 2023 at 7:15 Comment(1)
Thanks for attempting to help. Unfortunately, the environment is loaded with vscode, even before python is running. It has nothing to do with the Python code :-)Wordage

© 2022 - 2025 — McMap. All rights reserved.