What if you forget to add Python to the PATH variable?
A quick recovery plan if you forget to add Python to the PATH variable in Windows.
Do you remember the little radio button you ticked while installing Python 3 to your Windows machine?
What happens if you don't tick "Add Python 3.9 to PATH" during installation?
If you forget to add Python to the PATH variable, you won't be able to run Python in your command line from any other directory.
You will get a message saying
'python' is not recognized as an internal or external command.
This is because Windows is trying to search for Python in its PATH variable and is unable to find it.
What is the PATH variable in Windows?
The PATH is the variable on your system that your operating system uses to locate necessary executables from the command line. The PATH system variable can be set using System Utility in the control panel on Windows 10.
How to add Python to PATH on Windows post-installation?
Luckily, this situation can be easily fixed. All you have to do is add the directory path of your Python installation to the PATH variable in the Environment Variables of your system.
Step 1 - Refer to the Python installation window. It shows the directory where Python is installed.
Within that folder lies the executable file
python.exe. Copy the directory path.
It should be similar to -
Step 2 - Search for "Edit the system environment variables" in the search bar. You would be taken to System Properties.
Step 3 - Click on "Environment Variables" in System Properties.
Step 4 - Click on the "Path" variable and then click on "Edit".
Now here's the deal. Pay Attention!
Step 5 - Paste the directory path from Step 1 at the top of the list. You can use the "Move up" button to get it to the top. Now click on "Ok".
Now you can open up your command line and run
python. The Python interactive shell will open up.
Add the Scripts folder to the PATH variable
Here is a little extra tip!
One of the reasons for Python's huge popularity is the use of 3rd party libraries. So you would be installing many such libraries using pip. When you install any such library, it is installed into the Scripts folder inside your Python installation folder. Check out the path -
In order to be safe and to prevent any unwanted behaviors from your installed libraries, you should also add this path to your PATH variable in Windows.
There you go! Enjoy coding.