How to compile a python script to *.pyo

Matt Gerrans mgerrans at mindspring.com
Thu Apr 3 08:08:48 CEST 2003


I wanted to release some Python scripts that would be on a very large number
of systems and had to be reliable.   I wanted to have have at least a little
protection from tinkering, so used this little compiler:

# pycompile.py
if __name__=='__main__':
   import os
   exec( 'import %s' %
os.path.splitext(os.path.basename(os.sys.argv[1]))[0])

It was run as part of a release script which put together the package as
well as updating version stamps and whatnot.

Later I added a little MD5 self-check of the whole package (many Python and
other files), so the Python scripts can be included, but the first thing I
can ask someone when they report that my stuff "doesn't work" is to look at
a log file and see if it says anything about failing a self-integrity check.
That lets me know whether I'm the guilty party or they are, which is a good
starting point.  ;-)

By the way, this is the kind of script you only use on an internal project
that you use yourself, because it uses exec() on a command line parameter
(probably a bad idea in most cases) and has no error handling.   It could
also be improved to use glob(), for instance, if you want to compile many
scripts in one swell foop.






More information about the Python-list mailing list