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

10,223

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.

Share:
10,223

Related videos on Youtube

yart
Author by

yart

Updated on October 09, 2022

Comments

  • yart
    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
      michaelmeyer almost 11 years
      pyC: Compiled, pyO: Optimized, if you can't remember.
  • tarabyte
    tarabyte over 8 years
    I've been searching forever for the distinction between .pyo and .pyc files.
  • Ryan Haining
    Ryan Haining almost 5 years
    .pyo files will contain docstrings if created with -O, they will not have docstrings when using -OO