Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Accessing the system environment variables from Python

  • os.environ
import os

print(os.environ['HOME']) # /Users/gabor
print(os.environ.get('HOME')) # /Users/gabor

for k in os.environ.keys():
    print("{:30} {}".format(k , os.environ[k]))

os.environ is a dictionary where the keys are the environment variables and the values are, well, the values.