Using Python Environment Variables to Your Advantage
Environment variables are special settings that are part of the command line or terminal environment for your operating system. They serve to configure Python in a consistent manner. Environment variables perform many of the same tasks as do the options that you supply when you start Python, but you can make environment variables permanent so that you can configure Python the same way every time you start it without having to manually supply the option.
Most operating systems provide the means to set environment variables temporarily, by configuring them during a particular session, or permanently, by configuring them as part of the operating system setup. Precisely how you perform this task depends on the operating system. For example, when working with Windows, you can use the
Set command (see the blog post at for details) or rely on a special Windows configuration feature (see this blog post for setting the
Path environment variable as an example).
Using environment variables makes sense when you need to configure Python the same way on a regular basis. The following list describes the Python environment variables:
PYTHONCASEOK=x: Forces Python to ignore case when parsing
importstatements. This is a Windows-only environment variable.
PYTHONDEBUG=x: Performs the same task as the
PYTHONDONTWRITEBYTECODE=x: Performs the same task as the
PYTHONFAULTHANDLER=x: Forces Python to dump the Python traceback (list of calls that led to an error) on fatal errors.
PYTHONHASHSEED=arg: Determines the seed value used to generate hash values from various kinds of data. When this variable is set to
random, Python uses a random value to seed the hashes of
datetimeobjects. The valid integer range is 0 to 4294967295. Use a specific seed value to obtain predictable hash values for testing purposes.
PYTHONHOME=arg: Defines the default search path that Python uses to look for modules.
PYTHONINSPECT=x: Performs the same task as the
PYTHONIOENCODING=arg: Specifies the
utf-8) used for the
PYTHONNOUSERSITE: Performs the same task as the
PYTHONOPTIMIZE=x: Performs the same task as the
PYTHONPATH=arg: Provides a semicolon (;) separated list of directories to search for modules. This value is stored in the
sys.pathvariable in Python.
PYTHONSTARTUP=arg: Defines the name of a file to execute when Python starts. There is no default value for this environment variable.
PYTHONUNBUFFERED=x: Performs the same task as the
PYTHONVERBOSE=x: Performs the same task as the
PYTHONWARNINGS=arg: Performs the same task as the