Skip to content

Instantly share code, notes, and snippets.

@NinjaBunny9000
Created February 1, 2022 16:22
Show Gist options
  • Save NinjaBunny9000/2b968b8660403c97234e9c701d7f83bc to your computer and use it in GitHub Desktop.
Save NinjaBunny9000/2b968b8660403c97234e9c701d7f83bc to your computer and use it in GitHub Desktop.
There are two methods to using env variables with the `python-dotenv` package.. ## Method 1 This loads them into the "os" environment variables (for that particular instance of python that your code is executing in). - You can reference them as you
import os
from dotenv import load_dotenv, dotenv_values
# method 1)
load_dotenv() # load
# ref
os.getenv('VARIABLE_NAME', default='alternate_value') # specifying a default value to fall back on if env var is missing
os.env['VARIABLE_NAME'] # may return None
# method 2)
env = { **dotenv_values(".env") } # load
env['VARIABLE_NAME'] # ref
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment