Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of python-dotenv?

Need an example and please explain me the purpose of python-dotenv.
I am kind of confused with the documentation.

like image 896
Dev Jalla Avatar asked Jan 09 '17 11:01

Dev Jalla


People also ask

Why is dotenv required?

The dotenv is a zero-dependency module that loads environment variables from a . env file into process. env . Storing configuration in the environment separate from code is based on the Twelve-Factor App methodology.

What is .env file used for?

The . env file contains the individual user environment variables that override the variables set in the /etc/environment file. You can customize your environment variables as desired by modifying your . env file.

What is dotenv in Python?

python-dotenv Python-dotenv reads key-value pairs from a.env file and can set them as environment variables. It helps in the development of applications following the 12-factor principles.

How to interpolate variables using dotenv in Python?

Python-dotenv can interpolate variables using POSIX variable expansion. With load_dotenv (override=True) or dotenv_values (), the value of a variable is the first of the values defined in the following list: Value of that variable in the .env file. Value of that variable in the environment. Default value, if provided.

How to use Python-dotenv with load_dotenv?

The load_dotenv () function by default searches for a file called .env file located in the same directory as the Python file. You can override this behavior by passing the dotenv_path argument as such: You may want to have all the settings grouped into a Python dictionary. Python-dotenv makes it easy by using the dotenv_values () method:

How do I get the value of a dotenv variable?

With load_dotenv (override=True) or dotenv_values (), the value of a variable is the first of the values defined in the following list: Value of that variable in the .env file. Value of that variable in the environment.


2 Answers

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 └── settings.py 

Add the following code to your settings.py

# settings.py import os from os.path import join, dirname from dotenv import load_dotenv  dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path)  SECRET_KEY = os.environ.get("SECRET_KEY") DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD") 

.env is a simple text file. With each environment variables listed per line, in the format of KEY="Value", lines starting with # is ignored.

SOME_VAR=someval # I am a comment and that is OK FOO="BAR" 
like image 97
Will Avatar answered Sep 19 '22 14:09

Will


In addition to @Will's answer, the python-dotenv module comes with a find_dotenv() that will try to find the .env file.

# settings.py import os from dotenv import load_dotenv, find_dotenv  load_dotenv(find_dotenv())  SECRET_KEY = os.environ.get("SECRET_KEY") DATABASE_PASSWORD = os.environ.get("DATABASE_PASSWORD") 
like image 38
cannin Avatar answered Sep 20 '22 14:09

cannin