Last active
May 6, 2024 03:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is that for real