How do I run a Python program in the Command Prompt in Windows 7?


Question

I'm trying to figure out how to run Python programs with the Command Prompt on Windows 7. (I should have figured this out by now...)

When I typed "python" into the command prompt, I got the following error:

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

The first place I found when looking for help was this site: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows.

It was somewhat helpful, but the tutorial was written for Windows 2000 and older, so it was minimally helpful for my Windows 7 machine. I attempted the following:

For older versions of Windows the easiest way to do this is to edit the C:\AUTOEXEC.BAT >file. You would want to add a line like the following to AUTOEXEC.BAT:

This file did not exist on my machine (unless I'm mistaken).

Next, I tried this: (here: How do I run a Python program?)

Putting Python In Your Path

Windows

In order to run programs, your operating system looks in various places, and tries to match the name of the program / command you typed with some programs along the way.

In windows:

control panel > system > advanced > |Environmental Variables| > system variables -> Path

this needs to include: C:\Python26; (or equivalent). If you put it at the front, it will be the first place looked. You can also add it at the end, which is possibly saner.

Then restart your prompt, and try typing 'python'. If it all worked, you should get a ">>>" prompt.

This was relevant enough for Windows 7, and I made my way to the System Variables. I added a variable "python" with the value "C:\Python27"

I continued to get the error, even after restarting my computer.

Anyone know how to fix this?

1
157
11/23/2018 8:32:48 AM

Accepted Answer

You need to add C:\Python27 to your system PATH variable, not a new variable named "python".

Find the system PATH environment variable, and append to it a ; (which is the delimiter) and the path to the directory containing python.exe (e.g. C:\Python27). See below for exact steps.

The PATH environment variable lists all the locations that Windows (and cmd.exe) will check when given the name of a command, e.g. "python" (it also uses the PATHEXT variable for a list of executable file extensions to try). The first executable file it finds on the PATH with that name is the one it starts.

Note that after changing this variable, there is no need to restart Windows, but only new instances of cmd.exe will have the updated PATH. You can type set PATH at the command prompt to see what the current value is.


Exact steps for adding Python to the path on Windows 7+:

  1. Computer -> System Properties (or Win+Break) -> Advanced System Settings
  2. Click the Environment variables... button (in the Advanced tab)
  3. Edit PATH and append ;C:\Python27 to the end (substitute your Python version)
  4. Click OK. Note that changes to the PATH are only reflected in command prompts opened after the change took place.
226
11/29/2016 7:28:23 PM

Assuming you have Python2.7 installed

  1. Goto the Start Menu

  2. Right Click "Computer"

  3. Select "Properties"

  4. A dialog should pop up with a link on the left called "Advanced system settings". Click it.

  5. In the System Properties dialog, click the button called "Environment Variables".

  6. In the Environment Variables dialog look for "Path" under the System Variables window.

  7. Add ";C:\Python27" to the end of it. The semicolon is the path separator on windows.

  8. Click Ok and close the dialogs.

  9. Now open up a new command prompt and type "python"

It should work.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon