Reputation: 2509
Need an example and please explain me the purpose of python-dotenv.
I am kind of confused with the documentation.
Upvotes: 217
Views: 364783
Reputation: 19
Well, the way I am reading this code is slightly different than starriet suggests. It appears that
dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
load_dotenv(dotenv_path)
reads the .env file and sets/ clobbers key value pairs into environment variables in the shell that the python script is ALREADY executing in. Then when that shell dies, it takes the loaded OS environment variables with it to the grave. So technically, starriet is correct, the .env settings do not affect the shell you're actually in or other shells on the system, but nevertheless they are actually being set in the environment that python script is running in. That may or may not make a difference depending on the python script.
Upvotes: 0
Reputation: 49571
You could set the environment variables like this:
export PRIVATE_KEY=0X32323
and then read it with os
module.
import os
private_key=os.getenv("PRIVATE_KEY")
But this way, environment variable works only for the duration that shell is live. If you close the shell and restart it, you have to set environmental variable again. python-dotenv
prevents us from doing this repetitive work. For this, create .env
file and add variables in this format
PRIVATE_KEY=fb6b05d6e75a93e30e22334443379292ccd29f5d815ad93a86ee23e749227
then in the file you want to access environment variables:
import os
from dotenv import load_dotenv
# default directory for .env file is the current directory
# if you set .env in different directory, put the directory address load_dotenv("directory_of_.env)
load_dotenv()
load_dotenv()
will set the environment variables from .env
and we access with os
module:
private_key=os.getenv("PRIVATE_KEY")
Upvotes: 27
Reputation: 3948
This answer is to elaborate on 1) what load_dotenv
actually does under the hood, and 2) the relationship between the os.environ
and the OS's environment variables.
As the accepted answer describes, we can store some keys & values in the .env
file and load it using the load_dotenv
; then the os.environ
contains the key-value pairs in the .env
file.
Does this mean invoking load_dotenv
added an OS environment variable? - No.
Actually, what load_dotenv
does is quite the same as os.environ['key'] = 'value'
; see the source code.
But, os.environ['key'] = 'value'
does not add this key&value as an OS environment variable; They are only valid while the Python script is running.
In other words, os.environ
contains not only the OS environment variables but also one-time variables like these.
Upvotes: 3
Reputation: 361
Adding to @cannin's answer, if you want to specify the which file you want to find:
from dotenv import find_dotenv
from dotenv import load_dotenv
env_file = find_dotenv(".env.dev")
load_dotenv(env_file)
Upvotes: 13
Reputation: 3315
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")
Upvotes: 106
Reputation: 20098
If you're starting your app from a shell such as bash or zsh, then the point of .env management utilities like (npm) dotenv or python-dotenv becomes moot.
Here's an example of how to manage .env with bash that simply, directly, and safely addresses configuration as recommended by the 12-Factor App. It also requires no additional dependencies.
Given a project hosted under ~/projects/foobar/
, create an environment file in a safe location outside your project's space (e.g. ~/.envs/foobar/dev
). Its content may look something like this:
set -a
PROJECT=foobar
DB_NAME=foobar_dev
DB_PASSWORD=5ecret
CACHE_ENABLED=
DEBUG=yes
LOG=/tmp/foobar.log
...
set +a
Then create a symlink to that file from your project's space:
$ ln -s ~/.envs/foobar/dev ~/projects/foobar/.env
The project now has a .env
file symlinking to the actual file. When you source the symlink, all variables between set -a
and set +a
are exported to the environment.
$ source ~/projects/foobar/.env
And voila! If you run python from the same shell instance you sourced the environment file, you can retrieve the latter and update your config with it:
import os
config.update(os.environ)
The point of making .env
a symlink to ~/.envs/foobar/dev
is an added precaution to listing it in .gititgnore. If for whatever reasons the file were to be checked into version control, its contents would just show that it's a link to another file.
Upvotes: 8
Reputation: 5490
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 variable listed one per line, in the format of KEY="Value". The lines starting with # are ignored.
SOME_VAR=someval
# I am a comment and that is OK
FOO="BAR"
Upvotes: 316