6

I have Python 2.5 installed on Windows XP and a collection of scripts available in a certain directory. According to this article by Amanda Birmingham I can setup up a path for the Python executable and the one for where the scripts live. Which means I can do this:

c:\>python script.py

Is there a way I can perhaps register .py files so I can execute them like so:

c:\>script.py

without including a shebang or env line in the script.

Less typing equals good. :)

3 Answers 3

6

Yes :) That's actually FAQ #2:

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.

Edit: Of course, don't forget to add the Python install directory to the PATH, and to launch another instance of your command interpreter if you modify the systemwide or the user PATH and/or PATHEXT by means outside of that command interpreter.

Further edit: In case I misunderstood your question: I'm assuming you're only interested in launching python scripts without having to type in the .py extension from the cmd.exe command line. PATHEXT is a mechanism specific to the cmd.exe shell and other shells (as well as CreateProcess() and friends) won't care about it in the least.

9
  • Does the redirection bug the FAQ mentions for Win NT exist in Windows XP? I ask because Windows XP is based on NT, correct? May 4, 2009 at 20:07
  • No, that actually applies to Windows NT. 2000 and up were fixed. May 4, 2009 at 20:10
  • Edit: Just reconfirmed. On both 2000 and XP tacking :.PY to the end of PATHEXT works like a charm, .py scripts run without requiring the extension or any alteration of the actual script itself. Of course, ensure you've added the Python install directory to the PATH. May 4, 2009 at 20:17
  • Re: Running without the file extension: Not a big deal. But I see my problem now. It works as described in cmd.exe but silly me, I'm trying to run it that way in the bash shell using Cygwin. smack But thanks for your help. :) May 4, 2009 at 20:20
  • Welcome. I'm using msys for my rather limited Win32 needs (I crosscompile mostly everything from Linux) and the two share common roots. I'll try to figure out a solution, and if I get it to work I'll leave a comment to the question and we can test it under Cygwin. I'd rather not install a cyg distribution at the moment, still too traumatized from last time :) May 4, 2009 at 20:25
1

I had exactly the same issue with PHP.

http://php.net/manual/en/install.windows.legacy.index.php#install.windows.legacy.commandline (Edit: Seems command line usage for PHP is now "legacy")

One thing that I found was being able to use PHP scripts as filters ...

DIR C:\ | scripts\script | more

(for example - where scripts\script is a PHP script - NOTE : No PHP reference).

The registry change is required for this. Works for all of those interpreter type languages on windows (PHP, Python, Perl, Ruby).

0

PATHEXT will allow you to leave off ".py", but that's not what you are asking, right?

If you want to associate a file extension with an application on Windows, you can:

  1. Open an Explorer window.
  2. Choose "Folder options" from the Tools menu.
  3. Click the "File Types" tab.
  4. Click the "New" button (assuming that PY/PYC/PYW are not already there).
  5. Type in the extension (e.g. "py", "pyc" or "pyw") and click "OK"
  6. Click the "Change..." button.
  7. Choose "Select the program from a list"
  8. Find your Python interpreter (e.g. python.exe or pythonw.exe in C:\Python2X)
  9. Ok your way back out.

These are instructions for XP. I presume that Vista and Win7 are basically the same, although you probably get to the list in some other way.

1
  • Leaving off .py is exactly what Jonathon was asking. And considering that he's using Cygwin, I assume he's quite familiar with changing shell file associations. And the Pythin Win32 installer already associates the required extensions with the Python interpreter. May 5, 2009 at 5:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .