27

Possible Duplicate:
Set up Python on Windows to not type python in cmd

When I use python on Linux, or even Mac OS from command line, I take advantage of the shebang and run some of my scripts directly, like so: ./myScript.py. I do need to give this script executable permissions, but that is all.

Now, I just installed Python 3.1.2 on Windows 7, and I want to be able to do the same from command line. What additional steps do I need to follow?

Community
  • 1
  • 1
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
  • 2
    http://docs.python.org/faq/windows.html#how-do-i-make-python-scripts-executable – rwilliams Nov 21 '10 at 00:59
  • 2
    The Python installer should have associated `.py` files with the interpreter in the registry. You might have to manually add the extension to the PATHEXT environment variable. – martineau Nov 21 '10 at 01:58
  • 1
    @martineau Adding extension to the `PATHEXT` is needed only if one wants to run scripts **without** using extension. – Piotr Dobrogost Oct 03 '12 at 19:44
  • Did you try to run it the same way you're used to on Linux (by just typing `myScript.py`) before asking this question? – Piotr Dobrogost Oct 03 '12 at 20:16
  • 1
    @Piotr Dobrogost: Regardless of whether `PATHEXT` has had `.py` added to it and what that does, the important point is that in general one doesn't have to do anything after installing Python to be able to run scripts from the command-line (or by double-right-clicking on them. For that reason, I don't understand why the OP accepted the answer that they did. – martineau Oct 03 '12 at 20:23
  • @martineau I agree `PATHEXT` is not needed here and that's why after seeing you mentioned it I thought I would clarify this. As to accepted answer it states what you've already stated in your comment (about Python installer associating extensions); that's why I suspect OP didn't even try to run script by its name before asking question. – Piotr Dobrogost Oct 03 '12 at 20:33

1 Answers1

40

This sums it up better than I can say it:

http://docs.python.org/faq/windows.html

More specifically, check out the 2nd section titled "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.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
digitalfoo
  • 1,165
  • 13
  • 14
  • 4
    Does not work on my Windows 10 with my python 3.9.x. Running a python script just briefly pops open and then immediately closes a command window too fast for me to read whatever it's telling me, despite my python script being infinitely running design. – Geoffrey Anderson Aug 30 '21 at 18:30