Managing Environment Configuration Variables In Flask With Dotenv

  • Published: January 24, 2017


Often when developing apps with Flask we need to set up environment variables to keep sensitive information secure and out of version control.

This article describes how to set up environment config variables in Flask with python-dotenv and why we can’t rely in Flask’ instance folder.

dotenv versus instance folders

Flask introduces the concept of instance folder, designed to store sensitive information like credentials and passwords for your local environment, the main problem is that it can’t work with ephemeral filesystems like the one Heroku uses.

In this case the perfect fit for this is to use python-dotenv, so we define environment config variables in Heroku and locally we put them in /.env.


First we install it with pip install -U python-dotenv

$ pip install -U python-dotenv
Collecting python-dotenv
Installing collected packages: python-dotenv
Successfully installed python-dotenv-0.9.1


We add the new package to the requirements file.

$ pip freeze > requirements.txt


We make sure that we won’t add it to the source code version control adding it to .gitignore:

$ echo ".env" >> .gitignore

add variables

Add variables to .env, optionally copy all the environment variables from your heroku app to have this as a skeleton to fill with local variables:

$ heroku config -s >> .env

-s, --shell # output config vars in shell format

or simply add them manually to .env, for example:


use the new environment variables

Now we can use this environment variables in our Flask app.

Having the following typical structure:


I like to initialize my app in /myapp/__init.py__, but it should be the same if you add this to /myapp/

import os
from flask import Flask
from dotenv import load_dotenv

# load dotenv in the base root
APP_ROOT = os.path.join(os.path.dirname(__file__), '..')   # refers to application_top
dotenv_path = os.path.join(APP_ROOT, '.env')

tw_consumer_key = os.getenv('TWITTER_CONSUMER_KEY')


I find it useful to use dotenv for sensitive variables and custom paths, and maintain other variables in version control separated by server, like development, testing or production.

Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 ·