Can I "register" python scripts to execute on Windows?
Solution 1
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.
Solution 2
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).
Related videos on Youtube
Jeffrey Hulten
Web developer, a lot of end-to-end stuff. twitter.com/jonathonwatney
Updated on September 17, 2022Comments
-
Jeffrey Hulten over 1 year
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. :)
-
Jeffrey Hulten about 15 yearsDoes 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 about 15 yearsNo, that actually applies to Windows NT. 2000 and up were fixed.
-
Mihai Limbăşan about 15 yearsEdit: 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.
-
Jeffrey Hulten about 15 yearsRe: 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 about 15 yearsWelcome. 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 about 15 yearsNope, can't be done without patching bash, and maintaining a bash branch just to avoid typing .py or creating some symlinks borders on insanity :)
-
Jeffrey Hulten about 15 yearsThanks 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 about 15 yearsGreat, 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 about 15 yearsLeaving 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 about 15 yearsGlad 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...