ruxtain
ruxtain

Reputation: 194

How do I run a .py script directly from cmd?

Suppose I have a script called 'run.py', how can I do this?

C:\Users\Administrator>run

And this script will be executed. Please notice that I don't want a '.py' shown after 'run'. I am using python 3.3.5, and I tried putting 'C:/python33' into environment variable path. But it didn't work. It seems like it only works when it's a '.exe' file.

Thanks in advance.

Upvotes: 2

Views: 1618

Answers (1)

Alexander O'Mara
Alexander O'Mara

Reputation: 60577

You will need to add .py to your PATHEXT environment variable.

From the Python on Windows FAQ, How do I make Python scripts executable?

On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as ‘foo.py’. If you’d rather be able to execute the script by simple typing ‘foo’ with no extension you need to add .py to the PATHEXT environment variable.

The exact method for setting environment variables varies with different versions of Windows, but this link can probably help you out.

Upvotes: 4

Related Questions