Add python.exe to the path on Windows

2 min read 27-10-2024
Add python.exe to the path on Windows

If you have recently installed Python on your Windows machine but are having trouble running Python scripts from the command line, it’s likely that the Python executable (python.exe) is not added to your system’s PATH. The PATH is an environment variable that tells the operating system where to look for executable files.

In this article, we will guide you through the process of adding python.exe to the PATH in Windows, ensuring that you can run Python commands from any command prompt without needing to specify the full path to the executable.

Understanding the Problem

If you try to run Python from the Command Prompt and receive an error message saying that the command is not recognized, it indicates that the system cannot find the python.exe file. This is usually because the location of the Python installation has not been added to the PATH variable.

Here’s the common scenario:

  1. Original Problem Code:
    'python' is not recognized as an internal or external command, operable program or batch file.
    

Steps to Add python.exe to the PATH

Step 1: Locate Your Python Installation

First, you need to determine where Python is installed on your computer. By default, Python is usually installed in the following directory:

C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python<version>\

Replace <YourUsername> with your Windows username and <version> with the version number of Python you installed.

Step 2: Copy the Path to python.exe

Navigate to the directory where python.exe is located, and copy the full path to that folder. For example:

C:\Users\<YourUsername>\AppData\Local\Programs\Python\Python39\

Step 3: Modify the System PATH Variable

Now, follow these steps to add the path to the system PATH variable:

  1. Right-click on the Start menu and select System.
  2. Click on Advanced system settings on the left.
  3. In the System Properties window, click on the Environment Variables button.
  4. In the Environment Variables window, find the Path variable in the System variables section, and select it.
  5. Click Edit. In the Edit Environment Variable window, click New and paste the path you copied earlier.
  6. Click OK on all open windows to save the changes.

Step 4: Verify the Installation

To confirm that Python has been successfully added to your PATH, open a new Command Prompt window and type:

python --version

If you see the Python version number, congratulations! You’ve successfully added python.exe to your PATH.

Additional Tips

  • Restart the Command Prompt: After modifying the PATH, ensure that you open a new Command Prompt window, as changes won’t reflect in already open windows.
  • Check the PATH: You can view the current PATH settings by typing echo %PATH% in the Command Prompt.

Practical Example

For instance, if you want to execute a Python script named hello.py located in your Documents folder, you can do so without specifying the full path:

python C:\Users\<YourUsername>\Documents\hello.py

By having Python in your PATH, you can run this command from any directory, making it much more convenient.

Conclusion

Adding python.exe to your Windows PATH is a simple yet essential task for anyone working with Python. Once completed, you'll find that executing Python commands becomes much more streamlined and efficient.

Useful Resources

By following these instructions, you can enhance your productivity and ensure a smooth development experience with Python on your Windows machine. Happy coding!