1.5. Environment Variables and PATH¶
All running processes of a program, no matter the language in which it’s written, have a set of variables called environment variables that can store a string. Environment variables often hold systemwide settings that every program would find useful. For example, the TEMP environment variable holds the file path where any program can store temporary files. When the operating system runs a program (such as a command line), the newly cre- ated process receives its own copy of the operating system’s environment variables and values. You can change a process’s environment variables independently of the operating system’s set of environment variables. But those changes apply only to the process, not to the operating system or any other process.
I discuss environment variables in this chapter because one such vari- able, PATH , can help you run your programs from the command line.
1.5.1. Viewing Environment Variables¶
You can see a list of the terminal window’s environment variables by running set (on Windows) or env (on macOS and Linux) from the command line:
C:UsersAl>set ALLUSERSPROFILE=C:ProgramData APPDATA=C:UsersAlAppDataRoaming CommonProgramFiles=C:Program FilesCommon Files --snip-- USERPROFILE=C:UsersAl VBOX_MSI_INSTALL_PATH=C:Program FilesOracleVirtualBoxwindir=C:WINDOWS
The text on the left side of the equal sign ( = ) is the environment vari- able name, and the text on the right side is the string value. Every process has its own set of environment variables, so different command lines can have different values for their environment variables.
You can also view the value of a single environment variable with the echo command. Run echo %HOMEPATH% on Windows or echo $HOME on macOS and Linux to view the value of the HOMEPATH or HOME environment variables, respectively, which contain the current user’s home folder. On Windows, it looks like this:
C:UsersAl>echo %HOMEPATH% UsersAl
On macOS or Linux, it looks like this:
al@al-VirtualBox:~$ echo $HOME /home/al
If that process creates another process (such as when a command line runs the Python interpreter), that child process receives its own copy of the parent process’s environment variables. The child process can change the values of its environment variables without affecting the parent process’s environment variables, and vice versa.
You can think of the operating system’s set of environment variables as the “master copy” from which a process copies its environment variables. The operating system’s environment variables change less frequently than a Python program’s. In fact, most users never directly touch their environ- ment variable settings.
1.5.2. Working with the PATH Environment Variable¶
When you enter a command, like python on Windows or python3 on macOS and Linux, the terminal checks for a program with that name in the folder you’re currently in. If it doesn’t find it there, it will check the folders listed in the PATH environment variable.
This lengthy pathname requires a lot of typing, so instead I add this folder to the PATH environment variable. Then, when I enter python.exe , the command line searches for a program with this name in the folders listed in PATH , saving me from having to type the entire file path.
Because environment variables can contain only a single string value, adding multiple folder names to the PATH environment variable requires using a special format. On Windows, semicolons separate the folder names. You can view the current PATH value with the path command:
C:UsersAl>path C:Path;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem; --snip-- C:UsersAlAppDataLocalMicrosoftWindowsApps
On macOS and Linux, colons separate the folder names:
al@ubuntu:~$ echo $PATH /home/al/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/ bin:/usr/games:/usr/local/games:/snap/bin
The order of the folder names is important. If I have two files named someProgram.exe in C::raw-latex:`\WINDOWS`:raw-latex:`\system32` and C::raw-latex:`\WINDOWS`, entering someProgram.exe will run the program in C::raw-latex:`\WINDOWS`:raw-latex:`\system32` because that folder appears first in the PATH environment variable.
If a program or command you enter doesn’t exist in the cwd or any of the directories listed in PATH , the command line will give you an error, such as command not found or not recognized as an internal or external command . If you didn’t make a typo, check which folder contains the program and see if it appears in the PATH environment variable. ### Changing the Command Line’s PATH Environment Variable You can change the current terminal window’s PATH environment variable to include additional folders. The process for adding folders to PATH varies slightly between Windows and macOS/Linux. On Windows, you can run the path command to add a new folder to the current PATH value:
①C:UsersAl>path C:newFolder;%PATH% ②C:UsersAl>path C:newFolder;C:Path;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem; --snip-- C:UsersAlAppDataLocalMicrosoftWindowsApps
The %PATH% part ① expands to the current value of the PATH environment variable, so you’re adding the new folder and a semicolon to the beginning of the existing PATH value. You can run the path command again to see the new value of PATH② .
On macOS and Linux, you can set the PATH environment variable with syntax similar to an assignment statement in Python:
① al@al-VirtualBox:~$ PATH=/newFolder:$PATH ② al@al-VirtualBox:~$ echo $PATH /newFolder:/home/al/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/ bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Ther$PATH part ① expands to the current value of the PATH environ- ment variable, so you’re adding the new folder and a colon to the existing PATH value. You can run the echo $PATH command again to see the new value of PATH ②.
But the previous two methods for adding folders to PATH apply only to the current terminal window and any programs run from it after the addition. If you open a new terminal window, it won’t have your changes. Permanently adding folders requires changing the operating system’s set of environment variables.
1.5.3. Permanently Adding Folders to PATH on Windows¶
Windows has two sets of environment variables: system environment variables (which apply to all users) and user environment variables (which override the system environment variable but apply to the current user only). To edit them, click the Start menu and then enter Edit environment variables for your account , which opens the Environment Variables window, as shown in Figure 2-6.
Select Path from the user variable list (not the system variable list), click Edit, add the new folder name in the text field that appears (don’t forget the semicolon separator), and click OK.
This interface isn’t the easiest to work with, so if you’re frequently edit- ing environment variables on Windows, I recommend installing the free Rapid Environment Editor software from https://www.rapidee.com/. Note that after installing it, you must run this software as the administrator to edit system environment variables. Click the Start menu, type Rapid Environment Editor , right-click the software’s icon, and click Run as administrator.
From the Command Prompt, you can permanently modify the system PATH variable using the setx command:
C:UsersAl>setx /M PATH "C:newFolder;%PATH%"
You’ll need to run the Command Prompt as the administrator to run the setx command.

Figure 2-6: The Environment Variables window on Windows ## Permanently Adding Folders to PATH on macOS and Linux To add folders to the PATH environment variables for all terminal windows on macOS and Linux, you’ll need to modify the .bashrc text file in your home folder and add the following line:
export PATH=/newFolder:$PATH
This line modifies PATH for all future terminal windows. On macOS Catalina and later versions, the default shell program has changed from Bash to Z Shell, so you’ll need to modify .zshrc in the home folder instead.