Skip to content

Instantly share code, notes, and snippets.

@sebble
Last active September 6, 2016 10:40
Show Gist options
  • Save sebble/d54cc2faf5e3c40528fc0ccb0ecaadfa to your computer and use it in GitHub Desktop.
Save sebble/d54cc2faf5e3c40528fc0ccb0ecaadfa to your computer and use it in GitHub Desktop.
Bootstrap environment variables from `.env` file in Python 3

Storing application variables in a 'hidden' file called .env?

Need to load these variables for dev/testing but didn't start jupyter notebook with them already exported?

Just drop the following into a Python 3 cell.

## bootstrap environment variables from file
from configparser import RawConfigParser
from itertools import chain
from os import environ
config = RawConfigParser()
with open('.env','r') as f:
config.read_file(chain(("[DEFAULT]",), f))
for k,v in config.items('DEFAULT'):
environ[k.upper().replace('EXPORT ','')] = v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment