How do I get the updated value of .env after applying set_key?
Not sure if I’m misunderstanding something, but shouldn’t os.getenv() return the new value after I’ve changed it?
.env
TEST="123"
main.py
from dotenv import load_dotenv, set_key
import os
load_dotenv()
print(os.getenv("TEST"))
set_key(".env", "TEST", "456")
print(os.getenv("TEST"))
output
123
123
Looking back into .env, I do see that the value has been updated to "456". How do i get the second output to reflect the change?
>Solution :
You should use
os.environ["TEST"] = "456"
instead if you want to change the current environment variable.
set_key(".env", "TEST", "456")
will update the value in the .env file, so the new value will be accessible the next time the file is read or load_dotenv() is called