10

I am running python 2.7, I can run a program fine when I open the *.py file.

But when I go to cmd and type "python *.py any other args", it doesn't work, it says that python is not recognised. This is hard because I am trying to do things like sys.argv[], any help is great.

Thanks

Levon
  • 138,105
  • 33
  • 200
  • 191
Angus Moore
  • 4,051
  • 5
  • 18
  • 14

3 Answers3

17

You probably don't have the Python executable on your PATH. The PATH variable tells the command interpreter where to find the commands you are issuing.

If you are on a Unix/Linux system try

/usr/bin/python

Under Windows it may vary, so find where Python (python.exe specifically) was installed and specify that complete path to get the command to run. I think this will work though:

c:\Python27\python.exe

Under Unix issue this command at the prompt:

/usr/bin/python myprog.py  arg1 arg2

Under Windows:

c:\Python27\python.exe myprog.py arg1 arg2

and it should run.

The long term solution is to add the Python directory to your PATH variable.

Since you are using Windows, this How to add python.exe to Windows PATH might be helpful. Or this page specifically for Windows 7.

Levon
  • 138,105
  • 33
  • 200
  • 191
  • @AngusMoore No that won't work, try this instead: `c:\Python27\python.exe` – Levon Jun 13 '12 at 23:54
  • @AngusMoore For the "shortcut" see the last line in my answer :-) It is to add Python to your path. Once it is on your path, you can simply type `python` and it will work. Some installation programs will automatically adjust your `PATH` for you. What version of Windows are you using? And did you get it to work? – Levon Jun 14 '12 at 00:03
  • Windows 7, I am just about to try it – Angus Moore Jun 14 '12 at 00:05
  • @AngusMoore Should work, I tried this on Win 7 too. I added a link on how to add Python to your path in my answer. However, if that isn't helpful, googling for "How to add to your path in Windows" will bring up a bunch of hits for you. – Levon Jun 14 '12 at 00:08
  • @AngusMoore *NO* -- *please* don't do that. It'll create a mess. The proper way is to add to your path. It's not that diffucult, see the links I provided and go that way. I assume you got it to work with the full path right? – Levon Jun 14 '12 at 00:11
  • I will do that, there is already a program that has changed the path, it is Pro/ENGINEER schools edition. How does that affect it – Angus Moore Jun 14 '12 at 00:14
  • @AngusMoore Sorry, I don't know that program, but it's quite common for programs to add themselves to the path. – Levon Jun 14 '12 at 00:16
  • SO it will go back when I open that program, that's OK, I can change it back when I need it – Angus Moore Jun 14 '12 at 00:17
  • In the environment variables there was "Path" do I create one called "PATH"? – Angus Moore Jun 14 '12 at 00:23
  • I have read it now, I have put it in to the path variable, in system variables, and it still doesn't work. – Angus Moore Jun 14 '12 at 00:37
1

I hope, your problem really was the problem I think it is, because I (hopefully) had the same. I'm very sure, Levon's answer was right, so this is the n00b solution. For the CMD to recognize "python", you need to add something to the environment variable "Path". When you're done with the insturctions you can type "echo %PATH%" into the cmd and it should show you the variable value you just changed.

  1. Go to Computer > System Properties > Advanced Settings > Environment Variables
  2. Click the variable "Path" and add ;C:\Python27 to the variable value. Don't forget the ";" to separate the values.
  3. Confirm with OK in both windows and you're done.
1

You can solve this creating a bat file contenting:

cd %USERPROFILE%\AppData\Local\Programs\Python\Python37-32
cls
.\python

Then, you save on C:\Windows\System32 as python.bat