In today’s tutorial we’ll learn how to to insert Python into the path environment variable on Windows.
What is the Windows Path environment variable?
The path is an environment variable in the Windows operating system that points to certain directories that contain specific executable files that we often use. Directories that we typically find in a Windows path are C:\Windows, C:\Windows\system32, %JAVA_HOME%\bin etc’.
Why do we want Python in the Path and should we add it?
We want Python to be set as part of the Path so we can refer to the python.exe or to utilities that are part of the Python installation in a relative manner instead of having to write the full absolute path to the executable. In plain words, the meaning of having Python (or any other executable file) in the path, is that in order to invoke Python, we would rather type python than c:\Users\dave\python3.10\python.exe in our Windows command line.
Python Windows path error messages
We typically encounter error messages when trying to run the Python interpreter from the Windows command line prompt or using the Terminal tab in the popular Python Integrated development environment. Here are a few common errors:
‘python’ is not recognized as an internal or external command, operable program or batch file.
python : The term ‘python’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Error in PyCharm:
‘pip’ is not recognized as an internal or external command, operable program or batch file.
Same error, but this time when invoking pip. Similar error appear when invoking ipython, Mini conda / Anaconda etc’.
Similar Error in Visual Studio Code (VS) Terminal:
Adding Python to the Path automatically
The easiest way to ensure that Python is incorporated into the Path is set that accordingly when installing Python on your Windows computer. That said, even after you have already installed Python you can still use the Python installer to fix the path topic in two ways.
- First off, close any open Python files or the command prompt terminal windows.
- Then run the Python application setup executable file that matches the one installed in your computer.
- You’ll see the following dialog:
- Then select Modify, then hit Next.
- In the Advanced Options dialog mark the add Python to ENVIRONMENT variables checkbox as shown below. Then hit Install and and once done close the Setup dialog.
- Alternatively, run the Python executable file that matches the one installed in your computer.
- From the Modify Setup dialog, select Repair.
- If prompted by User Account Control hit Yes.
- The Repair Progress dialog will show the Python installation repair.
- Once done, hit Close.
The Python application and scripts folder were added to the Path.
Add Python to Path – manually
You can obviously edit your PATH environment manually .Kindly proceed as following to insert the Python directories to the path:
- On your Windows taskbar hit the magnifying glass button (search).
- In the Type here to search box, type Environment variables and hit Enter.
- The System Properties dialog will open up.
- In the bottom hit the Environment Variables button.
- In the System Variables section, highlight the Path entry.
- Hit Edit.
- Now, hit New.
- Add the absolute path to your Python.exe file. For example: C:\Python\Python3.10
- Now hit New again.
- Add the absolute path to your Python Scripts library.For example: C:\Python\Python3.10\Scripts
- Your dialog should look as following:
- Hit OK.
- Close the Environment Variables and System Properties dialogs.
- Restart your Python IDE or Command Prompt and invoke your Python application using the relative path as shown below:
- You are all set!
Adding PIP to the Path
The Python Package Manager utility PIP is accessible as an executable (exe) file right in your Python Scripts folder typically at <python_installation_directory>\Scripts.
If you path is not set correctly you will get the following error when trying to install a package or to update PIP:
You can easily solve the error by manually setting the Windows path or re-installing Python as shown in the sections above.