Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between pyc and pyo files in Python? [duplicate]

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?

like image 631
yart Avatar asked Jul 04 '13 16:07

yart


People also ask

What is the difference between PYC and Pyo?

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

What is the difference between py and PYC files in Python?

. py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.

What is the use of .PYC file in Python?

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.

Is PYC faster than py?

Loading code from serialized . pyc files is faster than parsing the . py file using ANTLR.


2 Answers

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

like image 93
Ryan Haining Avatar answered Oct 12 '22 14:10

Ryan Haining


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.

like image 45
Games Brainiac Avatar answered Oct 12 '22 16:10

Games Brainiac