Menu

add Python to PATH – How to add Python to the PATH environment variable in Windows?

1. What is the purpose of adding Python to the PATH environment variable?

Adding Python to the PATH environment variable in Windows allows you to run Python commands from any directory within the command prompt. Here are the steps to add Python to the PATH variable:

2. What is the PATH environment variable in Windows?

The PATH environment variable, or simplhy called as the PATH, is a list of directory locations that the operating system permananently maintains and looks up when it has to find executable scripts.

For example, when you type ‘python’ in the command prompt, Windows has to know that the ‘python’ command really refers to the python program that you have recently installed.

Adding Python to the PATH is the way of letting windows know that ‘Python’ that you type in command prompt, actually refers to the Python program.

3. How to add Python to PATH?

Step 1. Locate your Python installation directory.

Typically, Python is installed in the C:\Users\{Your Username}\AppData\Local\Programs\Python\Python{version} folder, where {version} represents your installed Python version (e.g., Python39 for Python 3.9).

If you go to the above directory, you should be able to locate the Python executable and run it to start the Python REPL.

Step 2. Copy the path to the Python installation directory, as well as the \Scripts subdirectory.

For example, if Python is installed in C:\Users\{Your Username}\AppData\Local\Programs\Python\Python39, copy the following paths:

C:\Users\{Username}\AppData\Local\Programs\Python\Python39

C:\Users\{Username}\AppData\Local\Programs\Python\Python39\Scripts

Step 3. Right-click on “This PC” (or “My Computer” in older versions of Windows) and select “Properties”.

Step 4. In the System Properties window, click on “Advanced system settings” on the left side.

Alternately, instead of steps 3 and 4, you can search for “Environment Variables” in the Start menu and select “Edit the system environment variables”

Step 5. In the System Properties dialog, go to the “Advanced” tab and click on the “Environment Variables” button in the bottom.

Step 6. In the Environment Variables dialog, find the “Path” variable under the “System variables” section, and click on the “Edit” button.

Step 7. In the “Edit environment variable” dialog, click on the “New” button, and paste the Python installation directory path that you copied in step 2. Repeat this step to add the \Scripts subdirectory path as well.

Step 8. Click “OK” to close the “Edit environment variable” dialog, “OK” again to close the “Environment Variables” dialog, and one last “OK” to close the “System Properties” dialog.

Step 9. Restart any open command prompt windows, or open a new one. Now, you should be able to run Python commands from any directory within the command prompt.

4. How to verify if Python is added to PATH?

To verify that Python has been added to the PATH variable, open a new command prompt and run:

python --version

If the output displays the Python version, then Python has been successfully added to the PATH variable.

5. How does the order of directories within PATH matter?

When you type python in the command prompt, it starts searching the various folders for Python in the same order in which it is written in the PATH.

Once it has found what it is looking (python in this case), it will stop searching and give back the object it found for the first time.

6. What about adding Python to PATH on Linux and MacOS?

Python comes preinstalled with both Linux and MacOS. But you might want a more recent version of Python which you can install from the download page.

So, once you’ve installed Python, take a note of the location when the Python executable is stored.

Then locate the .profile or .bash_profile in your home directory by typing the following command:

$ cd ~
$ ls -a

Then run the following to appends Python’s location to the PATH.

echo export PATH="<PATH_TO_PYTHON>:$PATH" >> ~/.profile

If you find a .bash_profile instead use it in place of ~/.profile in the above command.

You will have to close and restart the terminal for the change to take effect. Becasue, the terminal runs the .profile or .bash_profile script everytime your start the terminal.

Alternately, simply run the source ~/.bash_profile to re-run the changes we made to .bash_profile

Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science