Last active
March 2, 2022 09:21
-
-
Save arshamalh/16a42854cb43bbe48038c7431529a552 to your computer and use it in GitHub Desktop.
Python best way to import environment variables for development and production
This file contains hidden or 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
""" | |
Written by Arsham Arya | |
I'm not sure it's the best way, | |
So any contribution makes everyone happy! | |
""" | |
from os import environ | |
# pip install python-dotenv | |
from dotenv import dotenv_values | |
config = { | |
**dotenv_values(".env"), # load shared development variables | |
**environ, # override loaded values with environment variables | |
} | |
def get_config(name, default_value=None): | |
return config.get(name, default_value) | |
# ***** # | |
# Then, you can import this function into your files and use it like this. | |
get_config("SOME_SECRET_KEY", "SHKDGFLRwpG-default_secret_key") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment