Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using updated dotenv value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading