Environment variables in Python

To get an variable, retrieve if from os.environ


It’s just like a dictionary, so you can print all of the entries:


Just like a dictionary, the get() method will return None if the requested key isn’t present, so it never raises an KeyError, and can take a default value:

    print(os.environ.get('SPAM', 'EGGS'))

The os.getenv()` function behaves exactly the same way:

    print(os.getenv('SPAM', 'EGGS'))