Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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. :)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
Does the redirection bug the FAQ mentions for Win NT exist in Windows XP? I ask because Windows XP is based on NT, correct? –  Jonathon Watney May 4 '09 at 20:07
    
No, that actually applies to Windows NT. 2000 and up were fixed. –  Mihai Limbăşan May 4 '09 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. –  Mihai Limbăşan May 4 '09 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. :) –  Jonathon Watney May 4 '09 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 :) –  Mihai Limbăşan May 4 '09 at 20:25

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.

share|improve this answer
    
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. –  Mihai Limbăşan May 5 '09 at 5:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.