How to Access Environment Variables in Python

Environment variables

Environment variables are variables that are assigned values externally to the Python program. Developers usually set them on the command-line before invoking the Python executable. The operating system then makes these variables accessible to a Python program from within.

Environment variables exist for program flexibility. This way, a user may change specific parameters before program execution, and the program will be able to look into these parameters and modify its behavior dynamically. No code modification is required, and such use of environment variables is called program configuration.

Environment variables in Python

The mechanism of setting environment variables is platform-dependent. For that reason, they are made available through Python’s built-in os module, which abstracts away operating system dependent functionality.

Python runtime keeps all environment variables of the program in a dictionary-like os.environ object.

Note that the os.environ object is populated when the Python runtime loads the os module. If you try to change environment variables after the fact (for example, by exporting a new environment variable in the terminal emulator), it will not work.

Reading environment variables

Accessing environment variables in Python is done by performing dictionary-like operations on os.environ object.

>>> import os
>>> os.environ
environ({'HOME': '/Users/john', 'LANG': 'en_US.UTF-8', 'LOGNAME': 'john', 'OLDPWD': '/Users/john', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin', 'PWD': '/Users/john/python-tutorial', 'SHELL': '/bin/zsh', 'TERM': 'screen', 'TERM_PROGRAM': 'Apple_Terminal', 'TERM_PROGRAM_VERSION': '433', 'TERM_SESSION_ID': 'CDC91EF3-15D6-41AD-A17B-E959D97BC4B5', 'TMPDIR': '/var/folders/md/31nwv67j113d19z0_1287f1r0000gn/T/', 'TMUX': '/private/tmp/tmux-501/default,3319,0', 'TMUX_PANE': '%28' , 'USER': 'john', '_': '/usr/local/bin/python3', '__PYVENV_LAUNCHER__': '/usr/local/bin/python3'})
>>> os.environ['HOME']
'/Users/john'
>>> os.environ['LANG']
'en_US.UTF-8'

If you access an environment variable using a subscript [] notation, and that variable is undefined, you will get a runtime error.

>>> os.environ['I_DONT_EXIST']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'I_DONT_EXIST'

To avoid this problem, you can read from the os.environ object using get() interface. If a looked up environment variable has not been defined, get() returns None.

>>> result = os.environ.get('I_DONT_EXIST')
>>> print(result)
None

A convenience feature of the get() interface is that you can specify a default value to use in case a searched for environment variable doesn’t exist.

>>> os.environ.get('I_DONT_EXIST', 'I_AM_DEFAULT')
'I AM DEFAULT'

An alternative to os.environ.get() is use os.getenv() function`. Both functions work the same, and you may treat the latter as a convenience API.

Writing environment variables

Sometimes you need to change an environment variable from within your program. For example, it may happen if you need to modify the behavior of your application upon receiving a configuration network request. You change environment variables using subscript [] operator as if os.environ has been a standard dictionary.

>>> import os
>>> os.environ['LANG']
'en_US.UTF-8'
>>> os.environ['LANG'] = 'en_GB.UTF-8'
>>> os.environ['LANG']
'en_GB.UTF-8'

Note that environment variable values should of if string type. If you try to assign an integer or any other non-string value to an environment variable, you will get a runtime error.

>>> import os
>>> os.environ['LANG'] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 684, in __setitem__
    value = self.encodevalue(value)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 754, in encode
    raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not int