Run Python in cmd

74,049

Solution 1

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.

Solution 2

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

Solution 3

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.
Share:
74,049
Angus Moore
Author by

Angus Moore

Updated on December 18, 2020

Comments

  • Angus Moore
    Angus Moore over 3 years

    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
    Levon almost 12 years
    @AngusMoore No that won't work, try this instead: c:\Python27\python.exe
  • Levon
    Levon almost 12 years
    @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?
  • Angus Moore
    Angus Moore almost 12 years
    Windows 7, I am just about to try it
  • Levon
    Levon almost 12 years
    @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
    Levon almost 12 years
    @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?
  • Angus Moore
    Angus Moore almost 12 years
    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
  • Levon
    Levon almost 12 years
    @AngusMoore Sorry, I don't know that program, but it's quite common for programs to add themselves to the path.
  • Angus Moore
    Angus Moore almost 12 years
    SO it will go back when I open that program, that's OK, I can change it back when I need it
  • Angus Moore
    Angus Moore almost 12 years
    In the environment variables there was "Path" do I create one called "PATH"?
  • Angus Moore
    Angus Moore almost 12 years
    I have read it now, I have put it in to the path variable, in system variables, and it still doesn't work.