FAQ
It seems that I've finally found an elegant way to do #! functionality on win2k
and upwards:

instead of #! ...

put:

@python -x "%~f0" %* & goto :EOF

in the first line of your python script file.

Call the file *scriptname*.bat

If this file is located in your path, you should be to call it from
anywhere by writing on the commandline:

*scriptname*

or:

*scriptname*.bat

Redirection of iostreams and piping between programs should work as
expected, and not fail as it does when using .py files on the
commandline.


Notes:
for emacs users put this in the bottom of your scriptfile.
# Local Variables: ***
# mode:python ***
# End: ***

Explanation of commandline:
@python -x "%~f0" %*
(run python) (skip first line of sourcetext) from fully qualified name of
the file itself)

& goto :EOF
command separator jump to end of file


This commanline uses the command extensions that are provided in
win2000 so it won't work on win9x family of win32 operating systems. I
don't think they work in NT 4 either but I am not sure.


--

Vennlig hilsen

Syver Enstad

Search Discussions

  • Dale Strickland-Clark at Mar 14, 2002 at 11:38 am
    I'm sure you're very pleased with that but I don't see the point.

    If I want to run a Python program, I just type the name of it at a
    command prompt.

    A few prerequisites need to be in place but some of these happen
    during install:

    1. The directory containing the script needs to be in your PATH
    environment var or be the current directory.
    2. PATHEXT environment var needs to contain ".PY;.PYC;.PYW"
    3. The Python exes need to be associated with the Python file types.

    Once you get all the above right, you just type:

    C:\> wibble args

    and it runs:

    "C:\Python21\python.exe" "wibble.py" args

    This only works with Win NT/2K/XP. As for 95/98/ME, well it's easier
    not to bother, really.

    Disguising your Python programs as batch files is going to knacker any
    attempt your editor may make to help with syntax highlighting and
    suchlike.

    Also, FYI, I think all the command extensions you mention were
    introduced with NT 4.
    --
    Dale Strickland-Clark
    Riverhall Systems Ltd
  • Bengt Richter at Mar 14, 2002 at 8:24 pm

    On Thu, 14 Mar 2002 11:38:52 +0000, Dale Strickland-Clark wrote:
    I'm sure you're very pleased with that but I don't see the point.

    If I want to run a Python program, I just type the name of it at a
    command prompt.
    Yes, but that is not the key point of Syver's contribution.
    A few prerequisites need to be in place but some of these happen
    during install:

    1. The directory containing the script needs to be in your PATH
    environment var or be the current directory.
    2. PATHEXT environment var needs to contain ".PY;.PYC;.PYW"
    3. The Python exes need to be associated with the Python file types.

    Once you get all the above right, you just type:

    C:\> wibble args

    and it runs:

    "C:\Python21\python.exe" "wibble.py" args
    You are missing the key point, which is about redirection of i/o.
    If you tried to use your wibble thus:

    wibble args > outputfile

    You would get a zero length output file on NT4.
    Syver solved _that_ problem without using a separate wrapper.

    I would have been pleased to have thought of it, especially since
    I recently posted separate wrapping solutions. Plus, not long ago I
    saw a post using a similar technique with a linux shell in a cgi
    context, and I didn't make the connection. Syver either re-invented
    that (for windows) or made the connection and reformulated it. Either
    way, I think he deserves credit, not a comment like "I'm sure you're
    very pleased with that ..."
    This only works with Win NT/2K/XP. As for 95/98/ME, well it's easier
    not to bother, really.

    Disguising your Python programs as batch files is going to knacker any
    attempt your editor may make to help with syntax highlighting and
    suchlike.
    That may be a downside for editors that decide on the basis of file extension,
    but you are not obligated to do all your scripts this way ;-)

    Being able to invoke a script concisely and have its output
    redirectable is something you want for full flexibility e.g. in putting
    customized scripted build steps in the VC++ IDE for e.g., customizing
    building Python from source on windows.

    Being able to wrap without a separate file is a good thing IMO, although
    for scripts that are supposed to be verbatim (e.g. signed etc) portable to
    *nix, you'd want to continue using separate wrappers in windows.

    <interesting side note>
    If you use a hash, e.g. MD5, or PGP to sign/validate a script, should it
    be based on canonicalizing to unix format (i.e., \n vs \r\n or \r)?
    I imagine the XML folks must have decided something. ...but I don't have
    time to google every stray question that pops to mind ;-)
    </interesting side note>
    Also, FYI, I think all the command extensions you mention were
    introduced with NT 4.
    Yes, the technique works on NT4, which pleases me ;-)
    The cmd.exe batch command line syntax is really horrid, weak, and badly
    documented though, so it's nice to be able to transition to Python in one line!

    Regards,
    Bengt Richter

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 14, '02 at 1:07a
activeMar 14, '02 at 8:24p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase