How do I make a Python .bat executable file?

Gordon McMillan gmcm at hypernet.com
Mon Dec 27 09:33:48 EST 1999


Robert Schockley writes:

> What kind of 'wrapper' is needed to make a python script an
> executable .bat file in Windows? Is the she-bang (#!/...)  line
> required? I would appreciate any help. ~Rob~

shebang does nothing on WIndows.

Most people use the file extension association (.py -> 
python.exe) which the installer sets up for you. Unfortunately, 
file redirection is broken (in general) when using file 
associations, so:
 >python myscript.py >out.txt  # works
 >myscript.py >out.txt # doesn't

The more direct equivalent of the shebang trick would be (this 
version courtesy of Bruce Eckel):
----------------------------------------------------------
@echo off
rem = """
rem run python on this bat file. Needs the full path where
rem you keep your python files. The -x causes python to skip
rem the first line of the file:
python -x c:\aaa\Python\\"%0".bat %1 %2 %3 %4 %5 %6 %7 %8 %9
goto endofpython
rem """

# The python program goes here:

print "hello, Python"

# For the end of the batch file:
rem = """
:endofpython
--------------------------------------------------------------
...which I never use because none of my editors are smart enough to 
syntax colorize a Python script with a .bat extension.

- Gordon




More information about the Python-list mailing list