1import os
2
3# Set environment variables
4os.environ['API_USER'] = 'username'
5os.environ['API_PASSWORD'] = 'secret'
6
7# Get environment variables
8USER = os.getenv('API_USER')
9PASSWORD = os.environ.get('API_PASSWORD')
10
11# Getting non-existent keys
12FOO = os.getenv('FOO') # None
13BAR = os.environ.get('BAR') # None
14BAZ = os.environ['BAZ'] # KeyError: key does not exist.
15
1""" lists environment variables, and splits elements in path variable """
2import os
3
4
5for k, v in sorted(os.environ.items()):
6 print(k+':', v)
7print('\n')
8# list elements in path environment variable
9[print(item) for item in os.environ['PATH'].split(';')]