What is the difference between pyc and pyo files in Python?
Solution 1
.pyc
files are python files compiled to byte code by the interpreter. They are generated normally when a file is imported.
.pyo
are compiled byte code without line numbers, assertions, and some other things (possibly doc strings) for optimzation purposes.
when invoking the python interpreter, you may pass the -O
or -OO
option to generate a .pyo
file. Using -O
will throw out the line numbers, assertions, and some debugging info. -OO
will result in a .pyo
file also stripped of docstrings.
Solution 2
The difference between .pyo
and .pyc
is that .pyo
is optimised and that means that you will not be able to use certain features like docstrings. .pyc
is the whole deal, with no limitations.
Related videos on Youtube
yart
Updated on October 09, 2022Comments
-
yart over 1 year
I see that .pyc and .pyo file are both compiled python code. What is difference between them and when I should use one or another?
-
michaelmeyer almost 11 yearspyC: Compiled, pyO: Optimized, if you can't remember.
-
-
tarabyte over 8 yearsI've been searching forever for the distinction between .pyo and .pyc files.
-
Ryan Haining almost 5 years
.pyo
files will contain docstrings if created with-O
, they will not have docstrings when using-OO