Chances are you’ll want so as to add Python to PATH
if you happen to’ve put in Python, however typing python
on the command line doesn’t appear to work. Chances are you’ll be getting a message saying that the time period python
isn’t acknowledged, or chances are you’ll find yourself with the improper model of Python operating.
A typical repair for these issues is including Python to the PATH
environment variable. On this tutorial, you’ll learn to add Python to PATH
. You’ll additionally find out about what PATH
is and why PATH
is important for applications just like the command line to have the ability to discover your Python set up.
Be aware: A path is the tackle of a file or folder in your onerous drive. The PATH
surroundings variable, additionally known as simply PATH
or Path, is a listing of paths to directories that your working system retains and makes use of to seek out executable scripts and applications.
The steps that you simply’ll must take so as to add one thing to PATH
will rely considerably in your working system (OS), so make sure to skip to the related part if you happen to’re solely on this process for one OS.
Be aware that you should utilize the next steps so as to add any program to PATH
, not simply Python.
The best way to Add Python to PATH
on Home windows
Step one is to find the listing during which your goal Python executable lives. The trail to the listing is what you’ll be including to the PATH
surroundings variable.
To search out the Python executable, you’ll must search for a file known as python.exe
. The Python executable might be in a listing in C:Python
or in your AppData
folder, for example. If the executable have been in AppData
, then the trail would usually look one thing like this:
C:Customers<USER>AppDataLocalProgramsPython
In your case, the <USER>
half would get replaced by your presently logged-in consumer title.
When you’ve discovered the executable, make sure that it really works by double-clicking it and verifying that it begins up a Python REPL in a brand new window.
For those who’re struggling to seek out the proper executable, you should utilize Home windows Explorer’s search characteristic. The problem with the built-in search is that it’s painfully gradual. To carry out a super-fast full system seek for any file, an important different is Everything:

These paths highlighted in yellow, specifically these at WindowsApps
and Python310
, could be ideally suited candidates so as to add to PATH
as a result of they appear to be executables on the root stage of an set up. These highlighted in purple wouldn’t be appropriate as a result of some are a part of a digital surroundings—you possibly can see venv
within the path—and a few are shortcuts or inside Home windows installations.
You may additionally encounter Python executables which can be put in inside the folder for a distinct program. This is because of the truth that many purposes bundle their very own model of Python inside them. These bundled Python installations would even be unsuitable.
When you’ve positioned your Python executable, open the Begin menu and seek for the Edit the system surroundings variables entry, which opens up a System Properties window. Within the Superior tab, click on on the button Surroundings Variables. There you’ll see Consumer and System variables, which you’ll be capable of edit:
Within the part entitled Consumer Variables, double-click on the entry that claims Path. One other window will pop up exhibiting a listing of paths. Click on the New button and paste the trail to your Python executable there. As soon as that’s inserted, choose your newly added path and click on the Transfer Up button till it’s on the high.
That’s it! Chances are you’ll must reboot your laptop for the adjustments to take impact, however you need to now be capable of name python
from the command line.
For setting the PATH
surroundings variable from the command line, try the part on Configuring Environment Variables within the Windows Python coding setup guide. You can even discover directions within the supplemental supplies:
You may additionally need to arrange PATH
in your Linux or macOS machine, or maybe you’re utilizing Windows Subsystem for Linux (WSL). In that case, learn the subsequent part for the process on UNIX-based programs.
The best way to Add Python to PATH
on Linux and macOS
Since Python usually comes pre-installed on UNIX-based programs, the commonest downside on Linux and macOS is for the improper python
to run, moderately than not discovering any python
. That stated, on this part, you’ll be troubleshooting not with the ability to run python
in any respect.
Be aware: Relying in your explicit system, you will have a python
program for Python 2, and a python3
for Python 3. In different cases, each python
and python3
will level to the identical executable.
Step one is finding your goal Python executable. It ought to be a program which you can run by first navigating to the containing listing after which typing ./python
on the command line.
It is advisable prepend the decision to the Python executable with its relative path within the present folder (./
) as a result of in any other case you’ll invoke whichever Python is presently recorded in your PATH
. As you discovered earlier, this won’t be the Python interpreter that you simply need to run.
Typically the Python executable might be discovered within the /bin/
folder. But when Python is already within the /bin/
folder, then it’s almost definitely already on PATH
as a result of /bin/
is mechanically added by the system. If that is so, then chances are you’ll need to skip to the section on the order of paths within PATH
.
Because you’re most likely right here since you’ve put in Python nevertheless it’s nonetheless not being discovered once you sort python
on the command line, although, you’ll need to seek for it in one other location.
Be aware: An awesome search utility for shortly looking out giant folders is fzf. It really works from the command line and can search all information and folders inside your present working listing. So that you may seek for python
from your house listing, for example. fzf will then present you the paths that comprise python
.
That stated, it is perhaps that /bin/
has been faraway from PATH
altogether, during which case you may skip ahead to the part on mangaging PATH
.
When you’ve positioned your Python executable and are positive it’s working, be aware of the trail for later. Now it’s time to start out the method of including it to your PATH
surroundings variable.
First, you’ll need to navigate to your house folder to take a look at what configuration scripts you might have obtainable:
You need to see a bunch of configuration information that start with a interval (.
). These are colloquially generally known as dotfiles and are hidden from ls
by default.
One or two dotfiles get executed everytime you log in to your system, one other one or two run everytime you begin a brand new command-line session, and most others are utilized by different purposes for configuration settings.
You’re in search of the information that run once you begin your system or a brand new command-line session. They’ll most likely have names much like these:
.profile
.bash_profile
.bash_login
.zprofile
.zlogin
The key phrases to search for are profile and login. You need to, in idea, solely have one among these, however when you have a couple of, chances are you’ll must learn the feedback in them to determine which of them run on login. For instance, .profile
file on Ubuntu will usually have the next remark:
# This file is just not learn by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
So, when you have .profile
but additionally .bash_profile
, you then’ll need to use .bash_profile
.
You can even use a .bashrc
or .zshrc
file, that are scripts that run everytime you begin a brand new command-line session. Run command (rc) information are widespread locations to place PATH
configurations.
Be aware: Pedantically talking, rc information are typically for settings that have an effect on the feel and appear of your command-line immediate, not for configuring surroundings variables like PATH
. However you should utilize rc information in your PATH
configuration if you happen to want.
So as to add the Python path to the start of your PATH
surroundings variable, you’re going to be executing a single command on the command line.
Use the next line, changing <PATH_TO_PYTHON>
along with your precise path to the Python executable, and substitute .profile
with the login script in your system:
$ echo export PATH="<PATH_TO_PYTHON>:$PATH" >> ~/.profile
This command provides export PATH="<PATH_TO_PYTHON>:$PATH"
to the tip of .profile
. The command export PATH="<PATH_TO_PYTHON>:$PATH"
prepends <PATH_TO_PYTHON>
to the PATH
surroundings variable. It’s much like the next operation in Python:
>>> PATH = "/residence/realpython/apps:/bin"
>>> PATH = f"/residence/realpython/python:PATH"
>>> PATH
'/residence/realpython/python:/residence/realpython/apps:/bin'
Since PATH
is only a string separated by colons, prepending a worth includes making a string with the brand new path, a colon, then the previous path. With this string, you set the brand new worth of PATH
.
To refresh your present command-line session, you possibly can run the next command, changing .profile
with whichever login script you’ve chosen:
Now, you need to be capable of name python
from the command line straight. The following time you log in, Python ought to mechanically be added to PATH
.
For those who’re considering this course of appears a bit opaque, you’re not alone! Learn on for extra of a deep dive into what’s occurring.
Understanding What PATH
Is
PATH
is an surroundings variable that comprises a listing of paths to folders. Every path in PATH
is separated by a colon or a semicolon—a colon for UNIX-based programs and a semicolon for Home windows. It’s like a Python variable with an extended string as its worth. The distinction is that PATH
is a variable accessible by nearly all applications.
Applications just like the command line use the PATH
surroundings variable to seek out executables. For instance, everytime you sort the title of a program into the command line, the command line will search varied locations for this system. One of many locations that the command line searches is PATH
.
All of the paths in PATH
have to be directories—they shouldn’t be information or executables straight. Applications that use PATH
take every listing in flip and search all of the information inside it. Subdirectories inside directories in PATH
don’t get searched, although. So it’s no good simply including your root path to PATH
!
It’s additionally vital to notice that applications that use PATH
usually don’t seek for something besides executables. So, you possibly can’t use PATH
as a approach to outline shortcuts to generally used information.
Understanding the Significance of Order Inside PATH
For those who sort python
into the command line, the command line will look in every folder within the PATH
surroundings variable for a python
executable. As soon as it finds one, it’ll cease looking out. This is the reason you prepend the trail to your Python executable to PATH
. Having the newly added path first ensures that your system will discover this Python executable.
A typical concern is having a failed Python set up in your PATH
. If the corrupted executable is the primary one which the command line comes throughout, then the command line will attempt to run that after which abort any additional looking out. The short repair for that is simply including your new Python listing earlier than the previous Python listing, although you’d most likely need to clear your system of the unhealthy Python set up too.
Reordering PATH
on Home windows is comparatively simple. You open the GUI management panel and modify the order utilizing the Transfer Up and Transfer Down buttons. For those who’re on a UNIX-based working system, nevertheless, the method is extra concerned. Learn on to be taught extra.
Managing Your PATH
on UNIX-based Methods
Often, your first activity when managing your PATH
is to see what’s in there. To see the worth of any surroundings variable in Linux or macOS, you should utilize the echo
command:
$ echo $PATH
/usr/native/sbin:/usr/native/bin:/usr/sbin:/residence/realpython/badpython:/usr/bin:/sbin:/bin:/usr/video games:/usr/native/video games
Be aware that the $
image is used to inform the command line that the next identifier is a variable. The problem with this command is that it simply dumps all of the paths on one line, separated by colons. So that you may need to reap the benefits of the tr
command to translate colons into newlines:
$ echo $PATH | tr ":" "n"
/usr/native/sbin
/usr/native/bin
/usr/sbin
/residence/realpython/badpython
/usr/bin
/sbin
/bin
/usr/video games
/usr/native/video games
On this instance, you possibly can see that badpython
is current in PATH
. The best plan of action could be to carry out some PATH
archaeology and work out the place it will get added to PATH
, however for now, you simply need to take away it by including one thing to your login script .
Since PATH
is a shell string, you don’t have entry to handy strategies to take away elements of it, such as you would if it have been a Python list. That stated, you possibly can pipe collectively a number of shell instructions to attain one thing comparable:
export PATH=`echo $PATH | tr ":" "n" | grep -v 'badpython' | tr "n" ":"`
This command takes the record from the earlier command and feeds it into grep
, which, along with the -v
switch, will filter out any strains containing the substring badpython
. Then you possibly can translate the newlines again to colons, and you’ve got a brand new and legitimate PATH
string that you simply use instantly to exchange your previous PATH
string.
Although this is usually a helpful command, the best answer could be to determine the place that unhealthy path will get added. You could possibly attempt different login scripts or study particular information in /and many others/
. In Ubuntu, for example, there’s a file known as surroundings
, which usually defines a beginning path for the system. In macOS, that is perhaps /and many others/paths
. There can be profile
information and folders in /and many others/
that may comprise startup scripts.
The primary distinction between configurations in /and many others/
and in your house folder is that what’s in /and many others/
is system-wide, whereas no matter’s in your house folder might be scoped to your consumer.
It may usually contain a little bit of archeology to trace down the place one thing will get added to your PATH
, although. So, chances are you’ll need to add a line in your login or rc script that filters out sure entries from PATH
as a fast repair.
Abstract
On this tutorial, you’ve discovered learn how to add Python, or another program, to your PATH
surroundings variable on Home windows, Linux, and macOS. You additionally discovered a bit extra about what PATH
is and why its inside order is important to contemplate. Lastly, you additionally found the way you may handle your PATH
on a UNIX-based system, seeing because it’s extra advanced than managing your PATH
on Home windows.