How to Add Python to PATH on Windows, Linux, and Mac (2024)

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 (1)

Prerequisites

  • Python installed.
  • Command-line access to the system.

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.

How to Add Python to PATH on Windows, Linux, and Mac (2)

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.

How to Add Python to PATH on Windows, Linux, and Mac (3)

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

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

How to Add Python to PATH on Windows, Linux, and Mac (4)

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

How to Add Python to PATH on Windows, Linux, and Mac (5)

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.

How to Add Python to PATH on Windows, Linux, and Mac (6)

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.

How to Add Python to PATH on Windows, Linux, and Mac (7)

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.

How to Add Python to PATH on Windows, Linux, and Mac (8)

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.

How to Add Python to PATH on Windows, Linux, and Mac (9)

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.

How to Add Python to PATH on Windows, Linux, and Mac (10)

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.

How to Add Python to PATH on Windows, Linux, and Mac (2024)
Top Articles
Guild Mortgage Strategically Expands with Acquisition of Academy Mortgage
The Bradleys' Big Savings
Srtc Tifton Ga
Where are the Best Boxing Gyms in the UK? - JD Sports
Www.1Tamilmv.cafe
Bin Stores in Wisconsin
The Daily News Leader from Staunton, Virginia
Midflorida Overnight Payoff Address
Federal Fusion 308 165 Grain Ballistics Chart
South Park Season 26 Kisscartoon
Botanist Workbench Rs3
Www.craigslist Augusta Ga
Top Golf 3000 Clubs
Tiger Island Hunting Club
Jessica Renee Johnson Update 2023
Craigslist Jobs Phoenix
4302024447
Alaska: Lockruf der Wildnis
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Snow Rider 3D Unblocked Wtf
Chastity Brainwash
Log in or sign up to view
Spergo Net Worth 2022
[Cheryll Glotfelty, Harold Fromm] The Ecocriticism(z-lib.org)
Skip The Games Fairbanks Alaska
Marion City Wide Garage Sale 2023
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
Chelsea Hardie Leaked
Tom Thumb Direct2Hr
Turns As A Jetliner Crossword Clue
Miller Plonka Obituaries
Tripcheck Oregon Map
49S Results Coral
Kempsville Recreation Center Pool Schedule
Transformers Movie Wiki
The Venus Flytrap: A Complete Care Guide
JD Power's top airlines in 2024, ranked - The Points Guy
Serenity Of Lathrop - Manteca Photos
Craigslist Jobs Brownsville Tx
Merkantilismus – Staatslexikon
Michael Jordan: A timeline of the NBA legend
Craiglist Hollywood
Weather Underground Corvallis
Exam With A Social Studies Section Crossword
Iupui Course Search
War Room Pandemic Rumble
Booknet.com Contract Marriage 2
Accident On 40 East Today
Walmart Front Door Wreaths
Black Adam Showtimes Near Cinemark Texarkana 14
Adams County 911 Live Incident
Yoshidakins
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6089

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.