Working with environment variables
Environment variables are part of operating systems and affect system operations. Python has Python-specific variables that affect how Python functions, that is, the behavior of the Python interpreter. While they are processed before command-line options, the command-line switches will override environment variables if there is a conflict.
How to do it...
- Environment variables are accessed via Python's
os.environ
. - Because the
environ
object is a dictionary, you can specify a particular variable to view:
>>> import os >>> print(os.environ["PATH"]) /home/cody/anaconda3/bin:/home/cody/bin:/home/cody/ .local/bin:/usr/local/sbin:/usr/local/bin:/usr /sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
- Adding a new variable is as simple as follows:
>>> os.environ["PYTHONOPTIMIZE"] = "1"
How it works...
There are a large number of Python-specific environment variables available. Some of them are:
PYTHONHOME...