[lug] Python Compiler Question

Rob Riggs rob at pangalactic.org
Tue Apr 18 22:29:32 MDT 2000


Wayde Allen wrote:
> 
> I recently wrote a short Python program that seems to run OK, but what is
> interesting is that I don't seem to be getting the byte compiled version
> of the program after running it once.  In other words, I've got this
> program opencalc.py and after running it I don't get an opencalc.pyc
> version.  I was thinking that this should always happen.  What gives?

Python normally only compiles modules that are imported.
If it didn't, the Unix magic of "#!/usr/bin/env python" at
the beginning of your script goes away and you end up having
to run your scripts with "python ./foo.pyc" rather than a
simple "./foo.py".

Also, many python scripts won't actually end in .py -- you
might just call it "foo" -- in the same manner that Bourne
shell script filenames rarely end in .sh. (The convention
that I normally use is that all of my class modules end in
.py, but my Python executables do not have any extension.)

Note that the python interpreter compiles your script into
byte code on every invocation. But the script is so small
that the compile time isn't really noticible.

If you really want to compile your script, just run
'python /usr/lib/python1.5/compileall.py -l .' to compile
all of the Python files in the current directory. I don't
believe you will notice any difference in execution speed.

-- 
Rob Riggs
http://www.pangalactic.org/




More information about the LUG mailing list