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. :)
-
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.Jonathon Watney : Does the redirection bug the FAQ mentions for Win NT exist in Windows XP? I ask because Windows XP is based on NT, correct?Mihai Limbăşan : No, that actually applies to Windows NT. 2000 and up were fixed.Mihai Limbăşan : 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.Jonathon Watney : 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. :)Mihai Limbăşan : 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 : Nope, can't be done without patching bash, and maintaining a bash branch just to avoid typing .py or creating some symlinks borders on insanity :)Jonathon Watney : Thanks for taking a look. :) I guess if I want to avoid the additional typing I'll have to have a set of aliases or something.Mark Nold : Great, i'll check this out for Perl. I am surprised that this accepted answer hasn't got any votes except mine... i don't think we have a limited amount of votes peoples...Mihai Limbăşan : Glad to be of help, Mark. It did get votes, Jonathon voted it up. However, someone else who will probably go unnamed voted it down. Happens...From Mihai Limbăşan -
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:
- Open an Explorer window.
- Choose "Folder options" from the Tools menu.
- Click the "File Types" tab.
- Click the "New" button (assuming that PY/PYC/PYW are not already there).
- Type in the extension (e.g. "py", "pyc" or "pyw") and click "OK"
- Click the "Change..." button.
- Choose "Select the program from a list"
- Find your Python interpreter (e.g. python.exe or pythonw.exe in C:\Python2X)
- 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.
Mihai Limbăşan : 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.From Tony Meyer
0 comments:
Post a Comment