Idiom for running compiled python scripts?

Max Erickson maxerickson at gmail.com
Wed Mar 21 02:44:06 CET 2007


Mark <markbpan at mailinator.com> wrote:

...
> #!/usr/bin/env python
> from myprog import main
> if __name__ == "__main__":
>     main()
> 
> Of course this compiles myprog.py into myprog.pyc on first run as
> I am wanting.
> 
> I have one of these stubs for all my python scripts I've created
> so far. Is there not a better way? Do I have to create a separate
> stub each time? I find it a bit messy to require a pair of
> scripts for each utility and it also contributes some
> inefficiency. Given the above stub is so boilerplate, why does
> python not provide a general stub/utility mechanism for this?

I don't know of a better way to organize things, but as an 
alternative, you could have a script where you import each of the 
scripts that you want compiled, python will write the compiled files 
to disk when you run it(thus avoiding the need to split the other 
scripts). There are also the py_compile and compileall modules, which 
have facilities for generating byte code.

More here:

http://effbot.org/zone/python-compile.htm

under 'Compiling python modules to byte code'.


max




More information about the Python-list mailing list