In this article, We will understand, Why we are seeing the error “Python is not recognized as an internal or external command” and how to fix it
The “Python is not recognized as an internal or external command” error is caused as a result of the Python command in the Windows command prompt and its due to either incorrect python installation or python’s executable file is not found in the System’s environment variables
One of the common difficulties for new Python learners is setting up the programming environment. Specifically, getting Python to work on their local machine can be challenging, especially for Windows users. The error message python is not recognized as an internal or external command, operable program or a batch file can be caused due to variety of factors, including
- Issues with the System’s environment variables.
- System Environment Variable is not set.
- System Environment is not properly configured.
- Missing or Incorrect Python installation.
This article will explore several possible solutions for resolving this error message, which may also apply to similar error messages. Before going into depth, let’s first understand what Environment variables are.
Environment variables are a set of name-value pairs that define the environment in which processes, such as Python programs, run. These variables store information about the System’s environment, such as the location of certain files or directories. Environment variables can be accessed, modified, and removed at any time during the execution of the process.
Ensure Python is properly installed
The first step in troubleshooting this error is to ensure that Python is properly installed on your machine. To check if Python is installed. Open the Command Prompt and type in py or Python followed by the enter key. If Python is installed and configured correctly, you should see the Python version number and a prompt that looks like >>>. As you can see below:
On the other hand, if you receive an error message similar to Python or py is not recognized as an internal or external command, it means that Python is not installed or configured properly.
Note: If Python is not installed, you can download and install the latest version of Python from the official Python website.
Reason 1: Unable to find the Python executable file Location
The error that Python is not recognized as an internal or external command occurs when the computer cannot find the location of the Python executable file. This happens when the directory containing the file is not included in the PATH environment variable, which tells the System where to search for executable files.
To fix this error, the Python executable’s directory must be added to the PATH variable. There are several ways to do this, two of which are as follows:
Once the python installation is complete, you will need to add the Python executable to the System’s environment variables. This will allow the System to recognize the python command and execute it correctly. To add Python to the environment variables, follow these steps:
- Press the Windows key + X and select System.
- Click on Advanced system settings.
- Click on the Environment Variables button.
- Under System Variables, scroll down, find the Path variable, and click on Edit.
- Click on New and enter the path to the Python executable. This is typically located in the Python installation directory, such as C:\python setup\python-3.11.1-amd64.exe. ( Note: Consider the below Section to check the path to the Python executable).
To Check the path to the Python executable
Click OK to save the changes
Once you have added the Python executable to the environment variables, open a new Command Prompt window and type py or Python followed by the enter key. You should now see the Python version number and the >>> prompt, indicating that Python is now recognized as a command by the System.
Another method to set Python to the environment variables is as follows:
If Python is already installed on your Windows system, you can add it to the environment variables by following these steps:
- Double-click on the python setup file.
- In Modify Setup window, click on the Modify option.
- Now press Next.
- In the Advanced Options window, check the Add Python to environment variables option and click on the Install option.
In case Python is not installed in your System, then follow these steps:
- Download the Python installer from the official website and run it.
- During the installation process, in the Advanced Options window, check the Add Python to environment variables option.
- Complete the installation process.
Once the installation is complete, you should be able to access the Python interpreter from the command prompt by simply typing Python or py and pressing enter.
Reason 2: System’s environment variables misconfiguration
Another possible cause of this Python is not recognized as an internal or external command, operable program, or batch file error message is that the System’s environment variables are not configured correctly. This can happen if the environment variables are modified or deleted accidentally.
To check if this is the case, you can use the set command in the Command Prompt. This command will display a list of all the environment variables currently set on the System.
If the Path variable is missing or is not set correctly, you can add it back by following the steps outlined earlier in this article.
Reason 3: Incorrect or Outdated Python Version
In some cases, the issue may be caused by an incorrect or outdated version of Python. If you have multiple versions of Python installed on your machine, the System may be trying to use an incorrect version of Python.
To resolve this issue, you can use the where command in the Command Prompt. This command will display the location of all the Python executables on the System.
If you find that there are multiple versions of Python installed. (Type py –list in cmd to check this)
You can use the setx command to set the correct version of Python as the default. The setx command allows you to set a new value for an environment variable or create a new environment variable. To set the correct version of Python as the default, follow these steps:
- Open the Command Prompt and type where Python followed by the Enter key. This will display the location of all the Python executables on the System.
- Copy the path of the correct version of Python that you want to set.
- Type setx PATH “%PATH%;C:\python setup\path_to_python (replace path_to_python with the path you copied in step) and press Enter.
- To verify that the correct version of Python is set as the default, type py or Python in the command line and check the version number displayed.
In this tutorial, you were presented with a solution for addressing a common problem encountered by Windows users when attempting to run Python on their machines: the error message Python is not recognized as an internal or external command, operable program or batch file. This error occurs when the location of the Python executable file is not properly specified in the system PATH environment variable.
You learned how to overcome this error by specifying the full path to the python.exe file each time you wanted to run Python from the Command Prompt.
Additionally, you were shown how to add the file location to your system PATH, by editing the PATH environment variable and by using the Python installer. This ensures that the Python executable can be easily located and run without any issues in the future.
Good Luck with Your Learning !!