Can I "register" python scripts to execute on Windows?

5,088

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

Share:
5,088

Related videos on Youtube

Jeffrey Hulten
Author by

Jeffrey Hulten

Web developer, a lot of end-to-end stuff. twitter.com/jonathonwatney

Updated on September 17, 2022

Comments

  • Jeffrey Hulten
    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
    Jeffrey Hulten about 15 years
    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
    Mihai Limbăşan about 15 years
    No, that actually applies to Windows NT. 2000 and up were fixed.
  • Mihai Limbăşan
    Mihai Limbăşan about 15 years
    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.
  • Jeffrey Hulten
    Jeffrey Hulten about 15 years
    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
    Mihai Limbăşan about 15 years
    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
    Mihai Limbăşan about 15 years
    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 :)
  • Jeffrey Hulten
    Jeffrey Hulten about 15 years
    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
    Mark Nold about 15 years
    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
    Mihai Limbăşan about 15 years
    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
    Mihai Limbăşan about 15 years
    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...