Adding Python to Your PATH: A Step-by-Step Guide for Beginners

Learn how to easily add Python to your system’s PATH environment variable, allowing you to run Python from any directory and boosting your productivity as a developer. Follow our step-by-step guide now!

Updated October 18, 2023

As a Python developer, you may need to run Python scripts or applications from the command line. To do this, you need to have Python installed on your system and set up the environment variables properly. In this article, we will show you how to add Python to your system’s PATH environment variable, so you can run Python scripts and applications easily.

Why Do You Need to Add Python to Your PATH?

The PATH environment variable is a list of directories that contain executable files (such as .exe files on Windows or .sh files on Linux). When you open a terminal or command prompt, your system checks these directories for any executable files that match the command you enter. If Python is not in your PATH, you won’t be able to run Python scripts or applications from the command line.

How to Add Python to Your PATH on Windows

On Windows, you can add Python to your PATH by following these steps:

  1. Open the Control Panel and click on “System and Security” (or “System” in older versions of Windows).
  2. Click on “Advanced system settings” on the left menu.
  3. On the “Advanced tab”, click on the “Environment Variables” button.
  4. In the “System Variables” section, scroll down and find the “Path” variable, then click “Edit”.
  5. Click “New” and enter the path to your Python installation (e.g., C:\Python27\).
  6. Click “OK” on all the windows to close them.

Now, you can run Python scripts or applications from the command line by typing python followed by the name of the script or application. For example: python my_script.py.

How to Add Python to Your PATH on Linux and macOS

On Linux and macOS, you can add Python to your PATH by adding a line to your shell configuration file (usually ~/.bashrc or ~/.bash_profile). Here’s how:

  1. Open a terminal window and type echo $SHELL to see the name of your current shell. For example, if you are using Bash, the output will be something like bash.
  2. Type nano ~/.bashrc (or nano ~/.bash_profile on macOS) to open the shell configuration file in the Nano text editor.
  3. Add the following line to the end of the file:
export PATH=$PATH:/path/to/your/python/installation
  1. Replace /path/to/your/python/installation with the actual path to your Python installation (e.g., /usr/local/bin/).
  2. Press Ctrl + X to exit Nano, then press Y to save the changes.
  3. Restart your terminal or run source ~/.bashrc (or source ~/.bash_profile on macOS) to apply the changes.

Now, you can run Python scripts or applications from the command line by typing python followed by the name of the script or application. For example: python my_script.py.

Conclusion

Adding Python to your system’s PATH environment variable is an essential step for any Python developer. By following the steps outlined in this article, you can easily run Python scripts and applications from the command line on Windows, Linux, or macOS. Remember to customize the path variable to match the actual location of your Python installation. Happy coding!

Stay up to date on the latest in Coding Python with AI and Data Science

Intuit Mailchimp