Idiom for running compiled python scripts?

Mark mark at mailinator.com
Sat Mar 24 06:07:42 CET 2007


On Fri, 23 Mar 2007 07:47:04 -0700, Alex Martelli wrote:
> You can use
> 
> python -c 'import myscript; myscript.main()'
> 
> and variations thereon.

Hmmm, after all that, this seems to be close to what I was looking for.

Thanks Alex. Didn't find anything about this in your cookbook! (I'm just
starting reading it whole - best way to really learn the language I
think).

So the general purpose invoking bash script e.g. "runpy" is merely something 
like:

#################################################################
#!/bin/bash

if [ $# -lt 1 ]; then
    echo "usage: `basename $0` script.py [script args ..]"
    exit 1
fi

PROG=$1
DIR=`dirname $PROG`
MOD=`basename $PROG`
MOD=${MOD%.py}
shift
exec python -c "import sys; \
                sys.argv[0] = \"$PROG\"; \
                sys.path.append(\"$DIR\"); \
                import $MOD; $MOD.main()" $@
#################################################################

So I timed "~/bin/myscript.py myargs" against "runpy ~/bin/myscript.py
myargs" but got only maybe a couple of millisec improvement (using a
1000 line myscript.py which just immediately exits - to try and push the
advantage!).

So I agree - the ends do not justify the means here and I'll just
execute myscript.py directly. Still not sure why python does not provide
this as a "python --compile_and_run myscript.py" option though?! ;)



More information about the Python-list mailing list