Skip to content

Instantly share code, notes, and snippets.

@staltz
Last active May 6, 2024 03:15
Show Gist options
  • Save staltz/5525397 to your computer and use it in GitHub Desktop.
Save staltz/5525397 to your computer and use it in GitHub Desktop.
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
ENV_VARS_FILENAME = '.env'
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 += '/'
try:
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
@brouse36
Copy link

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