Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Call this from your Django settings file to import environment variables from '.env'. Useful if you use PyCharm and want an automatic solution to recognize your env variables.
import os
def import_env_vars(project_root):
"""Imports some environment variables from a special .env file in the
project root directory.
if len(project_root) > 0 and project_root[-1] != '/':
project_root += '/'
envfile = file(project_root+ENV_VARS_FILENAME)
except IOError:
raise Exception("You must have a {0} file in your project root "
"in order to run the server in your local machine. "
"This specifies some necessary environment variables. ")
for line in envfile.readlines():
[key,value] = line.strip().split("=")
os.environ[key] = value

This comment has been minimized.

Copy link

@brouse36 brouse36 commented May 6, 2013

Is that for real

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.