Created
February 1, 2022 16:22
-
-
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
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 | |
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