Using Python Environment Variables to Your Advantage

By John Paul Mueller

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 import statements. This is a Windows-only environment variable.
  • PYTHONDEBUG=x: Performs the same task as the -d option.
  • PYTHONDONTWRITEBYTECODE=x: Performs the same task as the -B option.
  • 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 str, bytes, and datetime objects. 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 -i option.
  • PYTHONIOENCODING=arg: Specifies the encoding[:errors] (such as utf-8) used for the stdin, stdout, and stderr devices.
  • PYTHONNOUSERSITE: Performs the same task as the -s option.
  • PYTHONOPTIMIZE=x: Performs the same task as the -O option.
  • PYTHONPATH=arg: Provides a semicolon (;) separated list of directories to search for modules. This value is stored in the sys.path variable 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 -u option.
  • PYTHONVERBOSE=x: Performs the same task as the -v option.
  • PYTHONWARNINGS=arg: Performs the same task as the -W option.