Every Operating System has a list of directories containing the executable files for the commands and one executable file for each command. Each executable file is saved by the name of the command itself. This list of directories is stored in a variable called Path.
In the case of Windows, whenever a command is typed in the Command prompt, the system searches for an executable file corresponding to that command in the Path variable and runs it. If there is a failure in finding the executable file for a command, the Command prompt throws an error message stating that the given command could not be recognized, as shown in the screenshot taken below:
You can get rid of this error if you write the complete directory of the executable file rather than writing just the command. Even though using this method can help you overcome this error, it is not considered an advisable approach.
So, here’s another way to remove this error, which is by adding the desired executable file to the path variable. This step is usually required to be done at the time of installation of applications like Python. To learn more about Python, you can also take up a free online python basic programs course and enhance your knowledge.
Steps for Adding Python to Path in Windows
It is possible to add the complete path of python.exe and store it in the path variable. Follow the steps given below to add Python to path:
Click on ‘This PC’
Go to ‘Properties’ on the menu bar
Choose the ‘Advanced Settings’ option given on the left hand side as shown in the figure below:
Now, click on the button named ‘Environment Variables’ present on the bottom right. An environment Variables window will open up as seen in the figure below:
You will find a ‘System variables’ section on the lower part of the ‘Environment Variables’ window. Choose the ‘Path’ variable from the given list of variables and click on the ‘Edit’ button. Take a look at the figure below for reference.
An ‘Edit environment variable’ window will pop us as shown in the figure given below. This window shows you the list of directories that the Path variable currently contains. Click on ‘New’ button given on the right hand side of the window.
Write down the location of the Python’s install directory as shown below and click on ‘OK’.
And Bingo! You have successfully added Python to the Path. You can check if it has been added properly by writing the Python command in your command line. You will not get the same error as before. Instead, the following message will be displayed on the screen. This ensures that Python has been successfully installed and added to the Path variable on your system.
Steps for Adding Python to Path in Mac
Like the Command prompt of Windows, you have the Terminal in Mac. When the installed Python is not added to the Path variable and you write the ‘python’ command in the Terminal, an error is raised with a message stating that the Command is not found, as can be seen in the figure below.
Hence, to add Python to the Path variable, follow through the given steps.
Open the Terminal on your MAC and give the following command:
The terminal will prompt you to enter your password.
Enter your password. This will open up a list of directories that are stored in the Path variable.
Now, after the last directory location, enter the path to the Python Install directory.
Press Ctrl + X to quit and further, press Y to save the changes to the Path variable. This will now allow you to access python directly from the terminal without being required to enter its full location.
Now, if you type in the command ‘python’ in the Terminal, no error will be raised. Instead, a the installed python version will be displayed, as shown in the figure below, which will confirm that Python has been successfully added to the Path.
This brings us to the end of the blog on how to add Python to Path. We hope you have a better understanding of the concept. To learn more such Python concepts, you can head over to Great Learning Academy and enroll in free online python courses today.