Skip to content

Using Python environment variables to your advantage

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.


As with options, most of these environment variables won’t make any sense right now. You can read through them to see what is available. You find some of the environment variables used later in the book. Feel free to skip the rest of this section and come back to it later when you need it.


Most operating systems provide the means to set environment variables tempo- rarily, by configuring them during a particular session, or permanently, by con- figuring 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 my blog post at http://blog. your-advantage/ for details) or rely on a special Windows configuration fea- ture (see my post at adding-a-location-to-the-windows-path/ for setting the Path environ- ment 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 envi- ronment 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
  • PYTHONDONTWRITEBYTECODE=x: Performs the same task as the -B




  • PYTHONFAULTHANDLER=x: Forces Python to dump the Python traceback (list of calls that led to an error) on fatal
  • PYTHONHASHSEED=arg: Determines the seed value used to generate hash values from various kinds of 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


  • PYTHONHOME=arg: Defines the default search path that Python uses to look for
  • PYTHONINSPECT=x: Performs the same task as the -i
  • 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
  • PYTHONOPTIMIZE=x: Performs the same task as the -O
  • PYTHONPATH=arg: Provides a semicolon (;) separated list of directories to search for This value is stored in the sys.path variable in Python.
  • PYTHONSTARTUP=arg: Defines the name of a file to execute when Python star There is no default value for this environment variable.
  • PYTHONUNBUFFERED=x: Performs the same task as the -u
  • PYTHONVERBOSE=x: Performs the same task as the -v
  • PYTHONWARNINGS=arg: Performs the same task as the -W



Typing a Command

After you start the command-line version of Python, you can begin typing commands. Using commands makes it possible to perform tasks, test ideas that you have for writing your application, and discover more about Python. Using the command line lets you gain hands-on experience with how Python actually works — details that could be hidden by an Interactive Development Environment (IDE) such as IDLE. The following sections get you started using the command line.



Telling the computer what to do

Python, like every other programming language in existence, relies on com- mands. A command is simply a step in a procedure. In Chapter 1, you saw how “Get the bread and butter from the refrigerator” is a step in a procedure for making toast. When working with Python, a command, such as print(), is simply the same thing: a step in a procedure.


To tell the computer what to do, you issue one or more commands that Python understands. Python translates these commands into instructions that the computer understands, and then you see the result. A command  such as print() can display the results onscreen so that you get an instant result. However, Python supports all sorts of commands, many of which don’t display any results onscreen but still do something important.


As the book progresses, you use commands to perform all sorts of tasks.  Each of these tasks will help you accomplish a goal, just as the steps in a pro- cedure do. When it seems as if all the Python commands become far too com- plex, simply remember to look at them as steps in a procedure. Even human procedures become complex at times, but if you take them one step at a time, you begin to see how they work. Python commands are the same way. Don’t get overwhelmed by them; instead, look at them one at a time and focus on just that step in your procedure.



Telling the computer you’re done

At some point, the procedure you create ends. When you make toast, the procedure ends when you finish buttering the toast. Computer procedures work precisely the same way. They have a starting and an ending point. When typing commands, the ending point for a particular step is the Enter key.

You press Enter to tell the computer that you’re done typing the command. As the book progresses, you find that Python provides a number of ways to signify that a step, group of steps, or even an entire application is complete. No matter how the task is accomplished, computer programs always have a distinct starting and stopping point.



Seeing the result

You now know that a command is a step in a procedure and that each command has a distinct starting and ending point. In addition, groups of commands and entire applications also have a distinct starting and ending



point. So, take a look at how this works. The following procedure helps you see the result of using a command:


1t .a Sctoapry of the Python command-line version.

You see a command prompt where you can type commands, as shown in Figure 3-1.

  1. Type print(“This is a line of ”) at the command line.

Notice that nothing happens. Yes, you typed a command, but you haven’t signified that the command is complete.

3es. sPErnter.

The command is complete, so you see a result like the one shown in Figure 3-2.








Figure 3-2:

Issuing commands tells Python

what to tell the computer

to do.



This exercise shows you how things work within Python. Each command that you type performs some task, but only after you tell Python that the command is complete in some way. The print() command displays data onscreen. In this case, you supplied text to display. Notice that the output shown in Figure 3-2 comes immediately after the command because this is an interactive environment — one in which you see the result of any given command immediately after Python performs it. Later, as you start creating applications, you notice that sometimes a result doesn’t appear immediately because the application environment delays it. Even so, the command is executed by Python immediately after the application tells Python that the command is complete.