Fix Python – What is the use of python-dotenv?


Asked By – Dev Jalla

Need an example and please explain me the purpose of python-dotenv.

I am kind of confused with the documentation.

Now we will see solution for issue: What is the use of python-dotenv?


From the Github page:

Reads the key,value pair from .env and adds them to environment variable. It is great of managing app settings during development and in production using 12-factor principles.

Assuming you have created the .env file along-side your settings module.

├── .env

Add the following code to your

import os
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), '.env')

SECRET_KEY = os.environ.get("SECRET_KEY")

.env is a simple text file with each environment variable listed one per line, in the format of KEY=”Value”. The lines starting with # are ignored.

# I am a comment and that is OK

This question is answered By – Will

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0