[Tutor] .pyc files and executables?

Rob Andrews rob@jam.rr.com
Wed, 15 Aug 2001 14:37:49 -0500


Lance E Sloan wrote:
> 
> I'm using Python 2.0 on a UNIX machine (Solaris 2.6) to write some
> CGIs.  I'm trying to find out more about .pyc files, because I think
> it may improve performance at least a little if I can get my CGIs
> saved as .pyc files.
> 
> My Python CGIs start out with a line like this:
> 
>   #!/usr/local/python/bin/python
> 
> I notice that .pyc files are not created for my main programs whether
> or not they have a .py extension.  However, .pyc files are created
> for modules (all have .py extensions) that my programs import.
> 
> Now, I've run Python interactively and created .pyc files for my
> main programs using the statement, "import myprogram.py".  Now I have
> myprogram.pyc.  It's not executable by itself, but I can run it if
> I use the command, "python myprogram.pyc".
> 
> What I want to know is this:  If I run myprogram.py, which starts up
> the interpreter, will Python see myprogram.pyc there and use it
> instead of reparsing the code in myprogram.py?
> 
If I'm not mistaken, that's precisely what will happen. If you invoke a
python file myprogram.py, it will run myprogram.pyc if it's more recent
than the .py version. If myprogram.py has been modified, presumably
because you changed something, the interpreter will grab your new
version.

Rob
-- 
A {} is a terrible thing to waste.
Useless Python!
http://www.lowerstandard.com/python