Questions about "compiled" Python (beginner)

H

HoneyMonster

I am new to Python (Python 2.7 on Linux). Research indicates that:

a) "Compiling" Python modules into intermediate bytecode marginally
improves load time.

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.

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

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?

Thanks.
 
T

Terry Reedy

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
directly.
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.
 
H

HoneyMonster

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
directly.


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.


They are created when imported into python started with -O (optimize).
That mainly deletes assertions and maybe something else.

Yes. Yes.

Thanks, Terry and Cousin Stanley for the clear explanation and useful URL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top