Python - Get And Set Env Vars
Last Updated: 1730Z 27NOV19 (Created: 1730Z 27NOV19)
# cribbed from [1]

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.

References:

  1. https://able.bio/rhett/how-to-set-and-get-environment-variables-in-python--274rgt5
  2. https://docs.python.org/3.5/library/os.html