8

Sometimes (in customer's PCs) I need a python script to execute in the Windows shell like a .CMD or .BAT, but without having the .py or .pyw extensions associated with PYTHON / PYTHONW.

I came out with a pair of 'quick'n dirty' solutions:

1)

"""
e:\devtool\python\python.exe %0 :: or %PYTHONPATH%\python.exe
goto eof:
""" 
# Python test
print "[works, but shows shell errors]"

2)

@echo off
for /f "skip=4 delims=xxx" %%l in (%0) do @echo %%l | e:\devtools\python\python.exe
goto :eof
::----------

# Python test
print "[works better, but is somewhat messy]"

Do you know a better solution? (ie: more concise or elegant)


Update:

based on @van answer, the more concise way I found (without setting ERRORLEVEL)

@e:\devtools\python\python.exe -x "%~f0" %* & exit /b

### Python begins....
import sys

for arg in sys.argv:
    print arg

raw_input("It works!!!\n")

###
1
  • I don't see anything wrong with your second solution (other than that || should be |). It's not super-elegant, but it gets the job done. Commented Mar 18, 2010 at 19:03

2 Answers 2

10

You can try to create a script what is both python and windows shell script. In this case you can name you file my_flexible_script.bat and execute it either directly or via python ....

See a content of pylint.bat file from pylint:

@echo off
rem = """-*-Python-*- script
rem -------------------- DOS section --------------------
rem You could set PYTHONPATH or TK environment variables here
python -x "%~f0" %*
goto exit

"""
# -------------------- Python section --------------------
import sys
from pylint import lint
lint.Run(sys.argv[1:])


DosExitLabel = """
:exit
exit(ERRORLEVEL)
rem """

It is similar to what you do, but has more compliant dual-script support.

5
  • Good one, the rem=""" trick! That's what I was looking for, thx
    – PabloG
    Commented Mar 18, 2010 at 19:20
  • 2
    This type of program is known as a polyglot (en.wikipedia.org/wiki/Polyglot_%28computing%29). Commented Mar 18, 2010 at 21:38
  • Why doesn't the @echo off trigger a Syntax Error exception? Commented May 31, 2010 at 15:16
  • @TimPietzcker From python --help; -x: skip first line of source, allowing use of non-Unix forms of #!cmd Commented Oct 29, 2011 at 18:50
  • "from future import" should be the first thing in the python script before any other import.
    – van
    Commented Oct 30, 2011 at 11:45
0

I use the following distutils/py2exe script to produce a single runnable executable file:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    console = [{'script': "MyScript.py"}],
    zipfile = None,
)

I see that MSVCR71.DLL gets copied into the dist directory as a result... but the chances are high that this dependancy is already on the target machine.

1
  • I should mention that I don't want to freeze the script, because my main purpose is using it like a "shell program on steroids", keeping it easily editable and modifiable
    – PabloG
    Commented Mar 18, 2010 at 19:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.