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?
A PYC file is the bytecode file generated and read from when no optimization level is specified at interpreter startup (i.e., -O is not specified). A PYO file represents the bytecode file that is read/written when any optimization level is specified (i.e., when -O or -OO is specified).
. py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.
pyc file contains the “compiled bytecode” of the imported module/program so that the “translation” from source code to bytecode can be skipped on subsequent imports of the *. py file. Having a *. pyc file saves the compilation time of converting the python source code to byte code, every time the file is imported.
Loading code from serialized . pyc files is faster than parsing the . py file using ANTLR.
.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With