Questions about "compiled" Python (beginner)

Terry Reedy tjreedy at
Sun Jan 29 14:01:01 EST 2012

On 1/29/2012 12:57 PM, HoneyMonster wrote:
> I am new to Python (Python 2.7 on Linux). Research indicates that:
> a) "Compiling" Python modules into intermediate bytecode marginally
> improves load time.

The improvement is larger the larger the file. You may notice that .pyc 
files are only created when a file is imported, not when it is run 

> b) The Python interpreter will use an already-prepared .pyc file if one
> exists in the same directory as the .py.
> That then, is presumably why for every .py file in my site-packages
> directory there is a corresponding .pyc file.

In 3.2+, .pyc files are tucked away in a __pycache__ directory, with a 
version indicator added to the names so one directory can be used with 
more than one version of python.

> Question 1: What then, are the .pyo files? I note that many of them are
> identical to the .pyc, but that some differ.

They are created when imported into python started with -O (optimize). 
That mainly deletes assertions and maybe something else.
> Question 2: What happens if the .py file is changed and the .pyc is thus
> made obsolete. Does the interpreter ignore the .pyc? If so, how does it
> know? By the timestamp?

Yes. Yes.

Terry Jan Reedy

More information about the Python-list mailing list