8

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?

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
yart
  • 7,515
  • 12
  • 37
  • 37

2 Answers2

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