DEV Community

Discussion on: Using .env Files for Environment Variables in Python Applications

Collapse
 
tazim404 profile image
Tazim Rahbar

Is there any way to add new environment variable into .env file from python code.

Collapse
 
thierrynjike profile image
Thierry Njike • Edited

you can write inside using this

with open("$path/to/.env", 'a+') as newenv:
    newenv.write("\n$varname=$varvalue")
load_dotenv()           #load vars again to consider the new added one

Enter fullscreen mode Exit fullscreen mode