Skip to content

Effortlessly Add Python Path to Your System

[

How to Add Python to PATH

Table of Contents

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. This tutorial will guide you through the process of adding Python to PATH, along with an explanation of what PATH is and why it is important for programs to find your Python installation.

How to Add Python to PATH on Windows

To add Python to PATH on Windows, follow these steps:

  1. Locate the directory where your Python executable is located. The path to the directory is what you’ll be adding to the PATH environment variable.
  2. Look for a file called python.exe in the directory. The Python executable could be in C:\Python\ or your AppData\ folder. For example, if the executable is in AppData\, the path would typically be: C:\Users\<USER>\AppData\Local\Programs\Python, where <USER> is your currently logged-in user name.
  3. Verify that the Python executable works by double-clicking it and checking if a Python REPL starts up in a new window.
  4. If you’re having trouble finding the executable, you can use Windows Explorer’s search feature or a third-party tool like Everything.
  5. Once you’ve located the Python executable, open the Start menu and search for “Edit the system environment variables”. This will open the “System Properties” window.
  6. In the “Advanced” tab, click on the “Environment Variables” button. This will display the “User” and “System” variables that you can edit.
  7. In the “User Variables” section, double-click on the “Path” entry. This will open a new window showing a list of paths.
  8. Click the “New” button and paste the path to the Python executable directory that you found in the previous steps.
  9. Click “OK” to save the changes.

How to Add Python to PATH on Linux and macOS

To add Python to PATH on Linux and macOS, follow these steps:

  1. Open a terminal.
  2. Check if Python is already added to PATH by typing python3 --version or python --version. If you see a version number returned, Python is already added to PATH.
  3. If you don’t see a version number, you can manually add Python to PATH by modifying the shell configuration file. For example, for the Bash shell, you can edit ~/.bashrc or ~/.bash_profile.
  4. Open the shell configuration file using a text editor.
  5. Add the following line to the file:
export PATH="/usr/local/bin:$PATH"

Make sure to replace /usr/local/bin with the path to the directory where the Python executable is located. This is usually the default location for Python installations on Linux and macOS.

  1. Save the file and close the text editor.
  2. Restart your terminal or run source ~/.bashrc or source ~/.bash_profile to apply the changes.
  3. You can now check if Python is added to PATH by typing python3 --version or python --version in the terminal.

Understanding What PATH Is

The PATH environment variable is a list of directories that your operating system uses to find executable scripts and programs. When you type a command on the command line, the operating system looks in each directory listed in PATH to see if the command can be found. If the command is found in one of the directories, it can be executed.

Understanding the Importance of Order Within PATH

The order of directories listed in PATH is important because the operating system searches for executables in the order they appear in the list. If there are multiple executables with the same name in different directories, the operating system will use the one found first in PATH. It is important to set up PATH correctly to ensure that the desired version of Python is used.

Managing Your PATH on UNIX-based Systems

On UNIX-based systems like Linux and macOS, PATH can be managed by modifying the shell configuration file as mentioned earlier. It is common to add custom directories to PATH to make it easier to run custom scripts and programs.

It is also possible to temporarily modify PATH by setting it in the current shell session using the export command. For example, you can temporarily add a directory to PATH by running:

Terminal window
export PATH="/path/to/directory:$PATH"

This change will only affect the current shell session and will be lost when the session is closed.

Conclusion

Adding Python to PATH allows you to run Python scripts and programs from any directory on your operating system. By following the steps outlined in this tutorial, you can ensure that Python is correctly added to PATH on Windows, Linux, and macOS. Understanding PATH and its importance will help you troubleshoot any issues related to running Python from the command line.