How to Add Python to PATH – Real Python
How to Add Python to PATH by Ian Currie Updated Sep 26, 2022 Reading time estimate 13m basics best-practices devops Table of Contents Remove ads Recommended Course Adding Python to PATH (52m) You may need to add Python to PATH if you’ve installed Python , but typing python on the command line doesn’t seem to work. You may be getting a message saying that the term python isn’t recognized, or you may end up with the wrong version of Python running.
A common fix for these problems is adding Python to the PATH environment variable . In this tutorial, you’ll learn how to add Python to PATH . You’ll also learn about what PATH is and why PATH is vital for programs like the command line to be able to find your Python installation. Note: A path is the address of a file or folder on your hard drive. The PATH environment variable, also referred to as just PATH or Path , is a list of paths to directories that your operating system keeps and uses to find executable scripts and programs.
The steps that you’ll need to take to add something to PATH will depend significantly on your operating system (OS), so be sure to skip to the relevant section if you’re only interested in this procedure for one OS. Note that you can use the following steps to add any program to PATH , not just Python. Try It Yourself: Get the code examples you need to add Python to your PATH on Windows, macOS, or Linux. Take the Quiz: Test your knowledge with our interactive “How to Add Python to PATH” quiz.
You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz How to Add Python to PATH Test your knowledge of the PATH variable and learn how to add Python on Windows, Linux, and macOS for easy terminal access. How to Add Python to PATH on Windows The first step is to locate the directory in which your target Python executable lives. The path to the directory is what you’ll be adding to the PATH environment variable.
To find the Python executable, you’ll need to look for a file called python.exe . The Python executable could be in a directory in C:\Python\ or in your AppData\ folder, for instance. If the executable were in AppData\ , then the path would typically look something like this: Language: Shell C:\Users\<USER>\AppData\Local\Programs\Python In your case, the <USER> part would be replaced by your currently logged-in user name.
Once you’ve found the executable, make sure it works by double-clicking it and verifying that it starts up a Python REPL in a new window. If you’re struggling to find the right executable, you can use Windows Explorer’s search feature. The issue with the built-in search is that it’s painfully slow. To perform a super-fast full system search for any file, a great alternative is Everything : Those paths highlighted in yellow, namely those at \WindowsApps and \Python310 , would be ideal candidates to add to PATH because they look like executables at the root level of an installation.
Those highlighted in red wouldn’t be suitable because some are part of a virtual environment—you can see venv in the path—and some are shortcuts or internal Windows installations. You may also encounter Python executables that are installed within the folder for a different program. This is due to the fact that many applications bundle their own version of Python within them. These bundled Python installations would also be unsuitable.
Once you’ve located your Python executable, open the Start menu and search for the Edit the system environment variables entry, which opens up a System Properties window. In the Advanced tab, click on the button Environment Variables . There you’ll see User and System variables, which you’ll be able to edit: In the section entitled User Variables , double-click on the entry that says Path . Another window will pop up showing a list of paths.
Click the New button and paste the path to your Python executable there. Once that’s inserted, select your newly added path and click the Move Up button until it’s at the top. That’s it! You may need to reboot your computer for the changes to take effect, but you should now be able to call python from the command line. For setting the PATH environment variable from the command line, check out the section on Configuring Environment Variables in the Windows Python coding setup guide .
You can also find instructions in the supplemental materials: Try It Yourself: Get the code examples you need to add Python to your PATH on Windows, macOS, or Linux. You may also want to set up PATH on your Linux or macOS machine, or perhaps you’re using Windows Subsystem for Linux (WSL) . If so, read the next section for the procedure on UNIX-based systems. Remove ads How to Add Python to PATH on Linux and macOS Since Python typically comes pre-installed on UNIX-based systems, the most common problem on Linux and macOS is for the wrong python to run, rather than not finding any python .
That said, in this section, you’ll be troubleshooting not being able to run python at all. Note: Depending on your particular system, you may have a python program for Python 2, and a python3 for Python 3. In other instances, both python and python3 will point to the same executable. The first step is locating your target Python executable. It should be a program that you can run by first navigating to the containing directory and then typing ./python on the command line.
You need to prepend the call to the Python executable with its relative path in the current folder ( ./ ) because otherwise you’ll invoke whichever Python is currently recorded on your PATH . As you learned earlier, this might not be the Python interpreter that you want to run. Often the Python executable can be found in the /bin/ folder. But if Python is already in the /bin/ folder, then it’s most likely already on PATH because /bin/ is automatically added by the system.
If this is the case, then you may want to skip to the section on the order of paths within PATH . Since you’re probably here because you’ve installed Python but it’s still not being found when you type python on the command line, though, you’ll want to search for it in another location. Note: A great search utility for quickly searching large folders is fzf . It works from the command line and will search all files and folders within your current working directory.
So you might search for python from your home directory, for instance. fzf will then show you the paths that contain python . That said, it might be that /bin/ has been removed from PATH altogether, in which case you might skip forward to the section on mangaging PATH . Once you’ve located your Python executable and are sure it’s working, take note of the path for later. Now it’s time to start the process of adding it to your PATH environment variable.
First, you’ll want to navigate to your home folder to check out what configuration scripts you have available: Language: Shell $ cd ~ $ ls -a You should see a bunch of configuration files that begin with a period ( . ). These are colloquially known as dotfiles and are hidden from ls by default. One or two dotfiles get executed whenever you log in to your system, another one or two run whenever you start a new command-line session, and most others are used by other applications for configuration settings.
You’re looking for the files that run when you start your system or a new command-line session. They’ll probably have names similar to these: .profile .bash_profile .bash_login .zprofile .zlogin The keywords to look for are profile and login . You should, in theory, only have one of these, but if you have more than one, you may need to read the comments in them to figure out which ones run on login. For example, .profile file on Ubuntu will typically have the following comment: Language: Shell # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login # exists.
So, if you have .profi
The rest of this page is on the live site.