How to Add Python to PATH on Windows, Linux, and Mac

December 28, 2023

Introduction

When running Python for the first time after the installation, the command may return an error or start a Python version different from the one you installed. The solution is to add the correct Python binary to the PATH variable.

In this article, learn how to add the Python binary to PATH on Windows, Linux, and macOS.

How to add Python to PATH on Windows, Linux, and Mac.

Prerequisites

What Is PATH?

PATH is a system variable that contains a list of directories in which the operating system looks for applications. The PATH value is a string of directories separated by colons (on Linux and macOS) or semicolons (on Windows). Below is an example of a PATH variable in Ubuntu:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

When a user types a terminal command without providing a command path, the system searches for the corresponding binary in the PATH directories. If the binary is in any of the directories, the system executes it.

How to Add Python to PATH on Windows

Use Windows System Properties to add Python's installation directory to the PATH variable. The steps below show how to perform this action using the GUI.

Step 1: Find Python Installation Directory

Before modifying the PATH variable, find the Python installation directory. By default, Python is in the Python[version-number] directory located at:

C:\Users\[username]\AppData\Local\Programs\Python

Note: AppData is a hidden directory. To access it, enable the Show Hidden Files option in File Explorer.

Follow the steps below to find and copy the directory address:

1. Open File Explorer.

2. Navigate to the Python directory.

3. Right-click the directory in the navigation bar.

4. Select Copy address as text.

Copying the address of the Python installation directory to the clipboard.

Step 2: Locate PATH Variable Options

The options to modify the contents of the PATH variable are in the Environment Variables section of Windows System Properties. To access the options:

1. Click the Start button.

2. Search for and select Edit the system environment variables.

Searching for the Edit the system environment variables option.

3. Select the Advanced tab in the System Properties window.

4. Click the Environment Variables button in the section's bottom-right corner.

The location of the Environment Variables button in the Advanced tab of System Properties.

5. Access the PATH options by double-clicking the Path item in the User variables section of the Environment variables window.

The location of the PATH variable in the Environment Variables window.

Step 3: Add Python Directory to PATH

The Edit environment variable window contains a list of directories previously added to PATH. To add the Python entry:

1. Select the New button in the upper-right corner. A new blank entry appears in the list.

2. Paste the address from Step 1 into the entry and press Enter.

3. Move the address to the top of the list by pressing the Move Up button.

4. Click OK to exit.

Adding an address to the PATH variable.

5. Ensure the PATH variable now contains the Python directory by using the echo command in PowerShell:

echo $env:path

The output shows that PATH contains the Python directory.

Viewing the PATH variable in PowerShell.

Note: If you use Command Prompt, view PATH with the echo %PATH% command.

How to Add Python to PATH on Linux and Mac

Due to the fundamental design similarities between the two systems, the procedure for appending the Python directory to PATH on Linux and macOS is the same. Edit the PATH variable by executing the steps below.

Step 1: Add Path

The export command allows you to change environmental variables such as PATH. However, the changes last only for the duration of the current terminal session.

To make changes permanent, add the export command to the .profile file using the syntax below:

echo export PATH="[python-path]:$PATH" >> ~/.profile

For example, the following command permanently adds the /home/marko/.localpython/bin directory to PATH:

echo export PATH="/home/marko/.localpython/bin:$PATH" >> ~/.profile

Note: Use an absolute path for the export command.

Step 2: Apply Changes

The operating system reads .profile on system startup. Restart the system for the changes to take effect. Alternatively, force the system to read .profile with the command below:

source ~/.profile

The command produces no output.

Step 3: View PATH in Linux and macOS

Confirm that the path to the Python binary has been added to the PATH variable by typing:

echo $PATH

The new directory appears first in the string.

Viewing the PATH variable in Terminal.

Order Within PATH

As previously mentioned, when a user types a terminal command, the system checks the PATH variable and searches the listed directories for the binary of the same name. However, it is important to mention that the system reads the directories from first to last and stops searching as soon as it finds the first matching binary.

Prepending the PATH variable with the directory containing the desired Python version ensures that the system reads it first and executes the correct binary. It is considered best practice, especially if you have more than one Python version installed on your system.

Placing the correct directory first in PATH makes the system ignore other Python installations, but they will still be part of the variable and may make it difficult to read. If there are Python installations on your PATH that you want to remove, refer to the sections below.

Managing PATH on Windows

Edit and remove PATH variable addresses in Windows from the Edit environment variable window mentioned in Step 3 of the How to Add Python to PATH on Windows section. To remove an address, select it and click the Delete button on the right side of the window.

Deleting an address from the PATH variable in Windows.

Use the Edit button to change the saved address and the Move Up and Move Down buttons to change the order of addresses within the PATH variable.

Managing PATH on Linux or Mac

Since PATH management on Linux and macOS is done using CLI, the procedure for editing and deleting PATH directories is slightly more complicated than on Windows. Follow the steps below to learn how to edit PATH on Linux and macOS.

1. View a tidy list of the directories that are part of the PATH variable by typing:

echo $PATH | tr ":" "\n"

The tr command takes the input from echo and replaces each colon with a new line, creating a more legible list of directories.

Formatting the path for better viewing experience.

2. Remove a directory from a path by using the following command:

export PATH=`echo $PATH | tr ":" "\n" | grep -v '[path-to-remove]' | tr "\n" ":"`

The command performs the following operations:

  • The tr command formats the echo output and passes it to the grep command.
  • When used with the -v option, grep removes the line containing the [path-to-remove] and passes the output back to tr.
  • tr reformats the output into a colon-separated string and assigns it as the value of the PATH variable.

Note: The method above only removes the path for the duration of the terminal session. To permanently remove an unneeded path, locate the file that exports it to PATH and remove the export line. Files that may contain the export command are .bashrc .zshrc, .bash_profile, .zprofile, .profile, and similar configuration files.

Conclusion

After reading this article, you should know how to add a directory containing a Python binary to the PATH variable and run Python without specifying its path. The article also explained how to edit and remove items from the PATH variable.

If you are new to Python, read our Python Data Types overview.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Initialize Dictionary in Python
August 17, 2023

Mastering different techniques for initializing a dictionary is crucial for streamlining code for various apps.
Read more
How to Change Working Directory in Python
August 1, 2023

Changing the working directory in Python is essential for efficient Python file handling and management.
Read more
How to Check Python Version
December 15, 2023

Certain applications may require a specific version of Python, so it is crucial to know which program version...
Read more
How to Comment in Python
November 25, 2019

Comments are lines that compilers and interpreters ignore that developers use to leave notes about the code function.
Read more