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?
Asked
Active
Viewed 1.1k times
8
-
2py*C*: *C*ompiled, py*O*: *O*ptimized, if you can't remember. – michaelmeyer Jul 04 '13 at 17:16
2 Answers
13
.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 optimization 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.

Ryan Haining
- 35,360
- 15
- 114
- 174
-
I've been searching forever for the distinction between .pyo and .pyc files. – tarabyte Nov 04 '15 at 01:02
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.

Games Brainiac
- 80,178
- 33
- 141
- 199
-
`.pyo` files will contain docstrings if created with `-O`, they will not have docstrings when using `-OO` – Ryan Haining Jul 23 '19 at 19:05