111

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\.

Jacob-Jan Mosselman
  • 801
  • 1
  • 10
  • 18
Dave Baxter
  • 1,111
  • 2
  • 8
  • 3
  • 1
    possible duplicate of [python is not recognized as an internal or external command](http://stackoverflow.com/questions/14433499/python-is-not-recognized-as-an-internal-or-external-command) – Andy Jul 09 '14 at 03:34
  • 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 – Scott Stensland Jun 23 '17 at 21:35
  • The default is for it to not do so, you can run in again to modify installation. – janat08 Feb 04 '18 at 11:26
  • 1
    See also https://stackoverflow.com/questions/23708898/pip-is-not-recognized-as-an-internal-or-external-command which is about `pip` but contains more answers and more up to date information for Python 3. – tripleee Aug 20 '20 at 08:45

15 Answers15

180

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.
sarnold
  • 102,305
  • 22
  • 181
  • 238
Pepe
  • 6,360
  • 5
  • 27
  • 29
  • 18
    why doesn't the python windows installer do this automatically like some other installers do? – NHDaly Jun 03 '13 at 21:30
  • 1
    The default is for it to not do so, you can run in again to modify installation. – janat08 Feb 04 '18 at 11:26
  • 4
    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, – WHOATEMYNOODLES Sep 09 '19 at 21:04
  • 3
    For Windows 10: Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables – mrdaddychops Jun 08 '20 at 14:49
  • 1
    It was actually much easier to just re-run the installer and select to add environment variables. – El Sordo Sep 02 '22 at 14:44
  • @NHDaly because path settings are supposed to be made redundant by the Python Launcher for Windows. I touch on this in my answer to a different question: https://stackoverflow.com/a/70830617/523612. – Karl Knechtel Feb 11 '23 at 05:04
57

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
MeSo2
  • 450
  • 1
  • 7
  • 18
Sam
  • 791
  • 6
  • 9
  • 3
    This worked for me when I could not edit the System Variables in the System Properties as they were read only. – Nicholas Murray Dec 17 '13 at 14:10
  • 1
    You can set `PATH` in the `User variable for %username%` of sysdm.cpl. Windows will append this `%PATH%` to the "system-wide" `%PATH%`. – brandeded Jan 02 '14 at 14:58
  • 1
    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... – Hack-R Feb 21 '17 at 17:22
34

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.

Udi
  • 29,222
  • 9
  • 96
  • 129
  • 2
    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 – Ajar Jun 01 '14 at 11:36
  • 2
    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) – Tadhg McDonald-Jensen Dec 22 '16 at 15:36
  • Apparently the installer associates .py files with python, but does not always add the installation folder to PATH. – Udi Dec 22 '16 at 19:45
  • It gives me: File "C:\python27\tools\scripts\win_add2path.py", line 48 print "Path(s) added:" ^ SyntaxError: invalid syntax -> error :( – Line May 12 '17 at 11:36
  • This only added the folder location to my User PATH variable. I had to copy the results into the System PATH variable. – mrdaddychops Jun 08 '20 at 14:45
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.

jeromej
  • 10,508
  • 2
  • 43
  • 62
bathMarm0t
  • 423
  • 3
  • 9
  • 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 Jul 22 '14 at 15:07
  • @caiosm1005 Proposed an edit to remove the dangerous part because the info itself might still be useful. – jeromej Mar 26 '15 at 13:05
  • 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. – Tom Sep 28 '15 at 02:34
  • You Saved Meee!!! My Windows 7 SP1 x64 Professional hates spaces after semicolons, now my python works like a breezee :D – Broken_Window Dec 21 '16 at 15:36
9

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.

Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
Alina Ristea
  • 91
  • 1
  • 1
9

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

Bakos Bence
  • 243
  • 5
  • 7
7

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

lode
  • 504
  • 6
  • 21
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.

Rook
  • 60,248
  • 49
  • 165
  • 242
5

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

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

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
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

1

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

John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • what if you skipped this step ? – Gel Jul 18 '21 at 12:27
  • 1
    @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 – John Lobo Jul 18 '21 at 12:31
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

Hamid Heydarian
  • 802
  • 9
  • 16
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...

user235913
  • 81
  • 1
  • 2
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.

Kevin
  • 113
  • 2
  • 8