Lesson 2, Topic 4
In Progress

Appendix B: PATH Environment Variable

Lesson Progress
0% Complete

Definition of Environment Variables

An operating system (OS) provides an environment in which computer programs can run. It also provides these programs with the configuration information they may need to function properly. This configuration information is called environment variables:

Environment variables contain useful information about the OS and the computer’s hardware, and computer programs are expected to recognize and access this information when needed. Here are some examples of environment variables in Windows:

To get the list of environment variables available on your OS, you can use the set command in Command Prompt, or the gci env: command in PowerShell (gci and env are short for Get-ChildItem and environment, respectively):

PATH Environment Variable

In Windows, to run a computer program from the Command Prompt or PowerShell, you must normally specify the full path of its executable file. For example, to run the Python launcher for Windows, you must enter either of these commands (depending on whether the launcher is installed for all users, or for the current user only):

C:\> C:\Windows\py.exe
C:\> C:\Users\<Username>\AppData\Local\Programs\Python\Launcher\py.exe

However, thanks to the PATH environment variable, the two commands above can be shortened to just py.exe. Moreover, with the PATHEXT environment variable, the py.exe command can be further shortened to just py:

C:\> py.exe
C:\> py

In fact, with the PATH environment variable you don’t need to specify the full path of an executable file to run it; you just need to specify its name, which is py.exe in our case. So, let us see how this works.

In the search box of the Windows taskbar, type Edit Environment Variables, then press the Enter key of your keyboard:

This will open a window, named Environment Variables, with two sections:

  • User variables for <Username>: this section contains the environment variables specific to the current Windows user (in the screenshot below, the user is named Workstation).
  • System variables: this section contains the environment variables of the Windows OS (i.e. system-wide environment variables).

Both sections contain a PATH environment variable; the first applies to the current user only, whereas the second is a system-wide variable that applies to all users of the Windows system. Double-click them, one by one. This will open the following windows:

User-specific PATH

System-Wide PATH

The two windows above show that the PATH environment variable is an ordered list of paths. These paths specify the directories that contain executable files.

When you run a computer program from the Command Prompt or PowerShell without specifying the full path of its executable file, they will search for it in the following directories, in this order:

  • The current directory.
  • The directories of the system-wide PATH, in the same order in which they are specified.
  • The directories of the user-specific PATH, in the same order in which they are specified.

As soon as the executable file is found, the search will stop and the file will be executed.

For example, when you run the Python launcher from the Command Prompt (C:\> py.exe), the following steps will occur:

  • The Command Prompt will search in the current directory, which is C:\. If the executable is not found, it will move to the next step.
  • The Command Prompt will search in the directories of the system-wide PATH. It will start with %SystemRoot%\system32. The %SystemRoot% part of the path will be replaced by the SystemRoot environment variable (the double percent signs indicate an environment variable). SystemRoot is the path of the directory containing the Windows OS, which is generally C:\Windows. So, the path will become C:\Windows\system32. If the executable is not found, Command Prompt will move to the next directory, which is %SystemRoot%, or C:\Windows. If the executable is found (which is the case when the Python launcher is installed for all Windows users), the search will stop and the file will be executed.

Another example is when you run the Python launcher from the Command Prompt without specifying the .exe extension (C:\> py). In this case, the steps described above will hold true but with these differences:

  • The search will be made for a file whose base name is py.
  • When a file with the py base name is found, Command Prompt will access the PATHEXT environment variable, which is a list of semicolon-separated file extensions:

These extensions, which are typically used by executable files, will be appended one by one, from left to right, to the py base name (i.e. py.com, py.exe, py.bat, etc.) and each time the result of the appending will be compared to the previously found file (i.e. py.exe). As soon as a match is found, the appending will stop and the file will be executed.

The PATH environment variable can be accessed and used by any program running on Windows, not only Command Prompt and PowerShell. For example, it is generally used by Integrated Development Environments (IDEs), which are code editors with extra features and tools. IDEs use PATH to locate the Python interpreter executable python.exe.

Adding Python Executables to PATH

It is recommended to add the paths of the Python launcher (py.exe), the Python interpreter (python.exe), and the Package installer for Python (pip.exe) to the PATH environment variable to avoid writing their full paths when running them from the CLI.

Note: The python.exe file should be added to PATH for two additional reasons:

  • As mentioned above, some IDEs access PATH to locate this file. They need it for the execution of Python code.
  • For consistency with many Python documents, which use the CLI commands python.exe and python to run the Python interpreter (instead of the py.exe and py commands).

Python executables can be added to PATH using either of these methods:

  • In Default installation, check the Add Python 3.9 to PATH
  • In Customized Installation, in the Advanced Options window, check the Add Python to environment variables
  • If Python is already installed on your computer, navigate to the executable file of the Python distribution that you want to modify and double-click it. In the new window that will open, click on Modify, then on Next. In the Advanced Options window, check the Add Python to environment variables
  • Open the Environment Variables window as described earlier in this section. Click on the user-specific Path environment variable. In the new window that will open, click on New and add the directories of the Python executables, one by one. Click OK when you finish:

User-specific PATH, before adding directories

User-specific PATH, after adding directories

Note 1: The directory of a file can be determined by right-clicking it, then copying the path next to Location:

Note 2: If the Python launcher is installed for all users in C:\Windows, then you don’t need to add it to PATH because this directory is by default included in the system-wide PATH.

Note 3: If you add more than one Python version to PATH, then only the executables of the uppermost version will be executed because paths are searched from top to bottom, in order. Here is an example:

In the example above, when you run the Python interpreter from the CLI, it is the version 3.10 that will be executed because its path precedes the path of the version 3.9:

C:\> python.exe
Python 3.10.0b1 (tags/v3.10.0b1:ba42175, May  3 2021, 20:22:30) [MSC v.1928 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> 

You can use the Move Up and Move Down buttons (see the screenshot above) to prefer one version over another. However, don’t add many versions to the PATH variable because this can slow down the overall search process.