117

I just downloaded and installed Python 2.7.2 to my laptop and I am trying to run some sample programs. My laptop is running Windows XP.

When I open a cmd window and type python I get:

'python' is not recognized as an internal or external command, operable program or batch file.

I am not a Windows person (mostly mainframes). However I am guessing that there is some command that I need to execute to define python as a command. Can anyone tell me what that command is?

The installer placed Python at C:\Python27\.


Prior to Windows 10 and Python 3.7, this problem would cause Python not to be found at all. However, nowadays on up-to-date systems, it will instead result in an offer from Microsoft to install Python from the Microsoft Store.

Users who already installed Python themselves from python.org ordinarily should not also get the Microsoft version; the offer is made to try to help users who are not computer-literate enough to do it themselves, and there can be conflicts between how the two versions are set up. At best, it is redundant and normally not useful.

Please see CMD opens Windows Store when I type 'python' for details on the up-to-date version of the problem.

5
  • 1
    possible duplicate of python is not recognized as an internal or external command Commented Jul 9, 2014 at 3:34
  • 1
    I find it unbelievable that the python installer msi on windows fails to set PATH to python ... is this failure justifiable ? on linux this would never happen Commented Jun 23, 2017 at 21:35
  • The default is for it to not do so, you can run in again to modify installation. Commented Feb 4, 2018 at 11:26
  • 1
    See also stackoverflow.com/questions/23708898/… which is about pip but contains more answers and more up to date information for Python 3. Commented Aug 20, 2020 at 8:45
  • @ScottStensland It is very deliberate about not doing so. Installing on Linux doesn't modify the PATH either - because the default way of organizing "installed" programs is such that it doesn't need to do so. But because Windows wants each program to have a separate sub-folder within Program Files (or a user-specific folder for user installs), it can't work the same way. Commented Feb 11, 2024 at 7:03

16 Answers 16

187

You need to add the python executable path to your Window's PATH variable.

  1. From the desktop, right-click My Computer and click Properties.
  2. In the System Properties window, click on the Advanced tab.
  3. In the Advanced section, click the Environment Variables button.
  4. Highlight the Path variable in the Systems Variable section and click the Edit button.
  5. Add the path of your python executable(c:\Python27\). Each different directory is separated with a semicolon. (Note: do not put spaces between elements in the PATH. Your addition to the PATH should read ;c:\Python27 NOT ; C\Python27)
  6. Apply the changes. You might need to restart your system, though simply restarting cmd.exe should be sufficient.
  7. Launch cmd and try again. It should work.
Sign up to request clarification or add additional context in comments.

7 Comments

why doesn't the python windows installer do this automatically like some other installers do?
The default is for it to not do so, you can run in again to modify installation.
Make sure to add it to the SYSTEM VARIABLES not USER VARIABLES, there are two lists, both with PATH in them, you need to select the bottom one named system variables. I am blind and it took me way too long to figure out why,
For Windows 10: Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables
It was actually much easier to just re-run the installer and select to add environment variables.
|
59

This is because the Python exec are not in the search path of your operating system. In windows, start CMD. Type in

setx PATH PythonPath

where PythonPath is usually C:\Python27 or C:\Python33 or C:\Users\<Your User Name>\AppData\Local\Programs\Python\Python37 depending on your Python version. After restarting the CMD, you should get see outcomes when typing

Python --version

3 Comments

This worked for me when I could not edit the System Variables in the System Properties as they were read only.
You can set PATH in the User variable for %username% of sysdm.cpl. Windows will append this %PATH% to the "system-wide" %PATH%.
Only works if you have support tools added to your path. If you know how to add those then you should know how to add Python...
35

Python comes with a small utility that fixes this. From the command line run:

c:\python27\tools\scripts\win_add2path.py

Make sure you close the command window (with exit or the close button) and open it again.

5 Comments

I first added it to the PATH and it didn't work. This script was the only thing that worked! Thanks! Works just as good on Python 3.4
ok wait... if people can't access python to run scripts how are you suppose to run this script to fix the path? (I ask as a mac user that has never run into this issue)
Apparently the installer associates .py files with python, but does not always add the installation folder to PATH.
It gives me: File "C:\python27\tools\scripts\win_add2path.py", line 48 print "Path(s) added:" ^ SyntaxError: invalid syntax -> error :(
This only added the folder location to my User PATH variable. I had to copy the results into the System PATH variable.
11

I had the same problem for a long time. I just managed to resolve it.

So, you need to select your Path, like the others said above. What I did:

Open a command window. Write set path=C:\Python24 (put the location and the version for your python). Now type python, It should work.

The annoying part with this is that you have to type it every time you open the CMD.

I tried to do the permanent one (with the changes in the Environmental variables) but for me its not working.

Comments

9

Just another clarification for those starting out. When you add C:\PythonXX to your path, make sure there are NO SPACES between variables e.g.

This:

SomeOtherDirectory;C:\Python27

Not this:

SomeOtherDirectory; C:\Python27

That took me a good 15 minutes of headache to figure out (I'm on windows 7, might be OS dependent). Happy coding.

4 Comments

It's dangerous to put your PATH that way. I'm sure somebody has just copied the entire thing and added to their own OS PATH that directory for Microsoft SQL Server.
@caiosm1005 Proposed an edit to remove the dangerous part because the info itself might still be useful.
Thanks a lot. I tried various attempts and finally this answer solved my problem "'python' is not recognized as an internal or external command..." while running a .bat file.
You Saved Meee!!! My Windows 7 SP1 x64 Professional hates spaces after semicolons, now my python works like a breezee :D
9

You can do it in python installer: enter image description here

Comments

7

emphasis: Remember to always RESTART the CMD WINDOW after setting the PATH environmental variable for it to take effect!

Comments

6

Go to Control Panel / System / "Advanced" tab / Enviromental Variables

Find variable called PATH in the lower list, and edit it. Add to the end C:\Python27

Open a new cmd window and try now.

Comments

6

in PowerShell enter this:

[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")

Close PowerShell and then start it again to make sure Python now runs. If it doesn’t, restart may be required.

enter image description here

Comments

2

Further to @Udi post this is what the script tried to do, but did not work with me.

I had to the set the following in the PATH nothing else.

C:\Users\hUTBER\AppData\Local\Programs\Python\Python35
C:\Users\hUTBER\AppData\Local\Programs\Python\Python35\Scripts

Were mine and now python works in the cmd

Comments

2

Easy. Won't need to get confused but paths and variables and what to click. Just follow my steps:

Go to the python installer. Run it. Out of the 3 options choose modify. Check py launcher. Next. Check "Add python to environment variables" Install.

Restart the cmd when finished and boom done

Comments

2

If you are trying to install python version python-3.9.6 then click the checkbox of Add Python 3.9 to PATH

enter image description here

2 Comments

what if you skipped this step ?
@Gel nyou can add environment path C:\Python39; in my case its installed in mentioned path .since i am using windows 8.1 and bit slower system using so to avoid i uninstalled and installed again to avoid conflict for editing
1

You could mark "Add Python to environment variables" on Advanced Options in Python installer as mentioned in the answers. In case you don't see the Advanced Options try running the Python installer as administrator.

Run as administrator

Then you would be able to see Advanced Options in the installation process.

Advanced Options

Comments

0

Make sure you click on Add python.exe to path during install, and select:

"Will be installed on local hard drive"

It fixed my problem, hope it helps...

Comments

0

Another helpful but simple solution might be restarting your computer after doing the download if Python is in the PATH variable. This has been a mistake I usually make when downloading Python onto a new machine.

After restarting my machine then Windows will often recognize Python in the PATH variable.

Comments

0

Old question but I didn't se any answers regarding Windows 11.

In Windows 11 there is something called App execution aliases and that seems to take precedence over your PATH variables. Search for it in the start menu and disable all entries regarding Python and you're good to go.

I had the paths set (Python 3.13.1) but it still didn't work to write python, it just opened the Microsoft Store, writing python --version gave the familiar unrecognized command error, py --version worked.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.