Skip to content

Instantly share code, notes, and snippets.

@arshamalh
Last active March 2, 2022 09:21
Show Gist options
  • Save arshamalh/16a42854cb43bbe48038c7431529a552 to your computer and use it in GitHub Desktop.
Save arshamalh/16a42854cb43bbe48038c7431529a552 to your computer and use it in GitHub Desktop.
Python best way to import environment variables for development and production
"""
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