import env python code example
Example 1: dotenv python
# Install dotenv via:
pip3 install python-dotenv
# Load .env file using:
from dotenv import load_dotenv
load_dotenv()
# Use the variable with:
import os
os.getenv("ACCESS_KEY")
Example 2: get environment variables python
import os
# Set environment variables
os.environ['API_USER'] = 'username'
os.environ['API_PASSWORD'] = 'secret'
# Get environment variables
USER = os.getenv('API_USER')
PASSWORD = os.environ.get('API_PASSWORD')
# Getting non-existent keys
FOO = os.getenv('FOO') # None
BAR = os.environ.get('BAR') # None
BAZ = os.environ['BAZ'] # KeyError: key does not exist.
Example 3: python install dotenv
pip install python-dotenv