When you try to run python from the Windows command prompt, you might get this error:
'python' is not recognized as an internal or external command,
operable program or batch file.
The error occurs because Windows can’t find the Python executable file ‘python.exe’ from the PATH variable.
There are two ways you can fix this error:
- Reinstall Python and check the ‘Add to Path’ option
- Add python.exe to path with
setx
command
In this tutorial, I will help you learn how to do both in practice.
1. Reinstall Python and check the Add to PATH option
If you already have Python installed on your computer, you can reinstall Python and check the ‘Add python.exe to PATH’ option.
Get the latest installer from python.org website, then make sure that you check the option ‘Add python.exe to PATH’ when running the installer:
First, uninstall the Python you have from the Control Panel, then reinstall the program with the option checked.
Restart the command prompt by closing and opening it again. Check if Python is now available by running the python --version
command:
The error is now fixed, and you can run python
from the command line without any error.
2. Using the setx command
If you don’t want to reinstall Python, you can also add the path to Python executable manually using the command line.
First, you need to get the location of Python executable on your computer. You can try running the py
command as follows:
py -c "import sys; print(sys.executable)"
C:\Users\nsebhastian\AppData\Local\Programs\Python\Python310\python.exe
If the py
command doesn’t work, then you need to find the .exe
file location manually. Use solution #1 if you can’t find the location.
Next, you need to run the setx
command to add the location to your PATH environment variable:
setx path "%PATH%;<your path here>"
Replace <your path here>
with the absolute path to your python.exe
file:
setx path "%PATH%;C:\Users\nsebhastian\AppData\Local\Programs\Python\Python310"
This should add the Python executable to the PATH, and you should be able to run python
commands from the command line now.
Conclusion
The error 'python' is not recognized as an internal or external command, operable program or batch file.
occurs when Windows can’t find your Python executable file from the PATH variable.
To resolve this error, you need to add the absolute path to the python.exe
file in the PATH variable.
Once added, test if Python is working by running the python --version
command. Now you know how to solve this error. Great work! 👍