[Tutor] .py vs .pyc

Alan Gauld alan.gauld at btinternet.com
Thu Apr 19 19:28:38 CEST 2012

On 19/04/12 15:47, Max S. wrote:
> Could anyone tell me why I should use a .pyc file rather than a .py?

You don't, python handles that for you.
They only come into effect on import statement. When Python imports the 
module it will use the pyc if available (and more recent that the .py).
Don't try to second guess this just let Python do what it does.

>   After doing some research, I have found that a .py file is first
> precompiled and then run, while a .pyc file is already precompiled and
> is simply run.  But unless I'm mistaken, it seems that a .pyc is no
> faster or better than a .py file.

The pyc file will be imported faster because python will not need to do 
the initial precompile. Other than that small improvement it should make 
no difference.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list