An approach to using environment variables in Python
Enviroment variables are fundamentally important for any production ready software. There are tons of references about the reasons and most of them are related to the fact that it is a terrible idea to write access keys in the code, even more if the code is shared through Git or any similar service. Some other reasons have to do with the different set of configuration variables a team might have, and the need of them being interchangeable effortlesly without modifying the code.
So the target of this post is to share a practical way to deal with environmental variables in Python production code.
Code structure
Let’s assume this code structure. A project directory that contains an app project, the requirements.txt file and the .env
file. Inside the app file there is a modules directory and a config.py file.
project
- app
- __init__.py
- modules
- module1.py
- main.py
- config.py
- requirements.txt
- .env
The .env
file looks like this.
STAGE=production
APP_KEY=1234
The problem
In module1.py
we need the contents of the STAGE
and APP_KEY
variables. Python has the function os.getenv()
function, which does the job… Whenever the enviroment variable is set. So the question is:
How can we read the environment variables from the .env file and set them so that
os.getenv()
can read it?
And the answer is to use the dotenv library.
The solution
Assuming you are working with a virtualenv you can install the dotenv library with the command below.
pip install python-dotenv
Now let’s configure dotenv using the config.py
file like this.
from dotenv import load_dotenv
load_dotenv(verbose=True)STAGE = os.getenv('STAGE', 'production')
APP_KEY = os.getenv('APP_KEY')
So, what did just happen?
Precisely what was intended: after loaded dotenv looked for the.env
file at the root of the project (next to the requirements.txt file), read the lines containing the environment variables and set them into the environment so that os.getenv()
can read them. Then STAGE
and APP_KEY
were imported byos.getenv()
and voila, both enviroment variables are available at config.STATE
and config.APP_ENV
respectively.
So the remaining step is to import the config module and make use of the environment. Let’s look at module1.py
.
import config
stage = config.STAGE
app_key = config.APP_KEY
Since both enviroment variables were already set they are constants now and can be used anywhere in the project by importing config.py
.
A final note
With this approach you can share all your code anywhere as long as you keep safe your .env
file and you can change this as you wish without modifying the code. This procedure is useful when using Python with Docker, just add an instruction to copy .env
in your Dockerfile and make sure dotenv is installed.