Python. pyc file function

Asked

Viewed 1,523 times

7

If the Python language is interpreted, why are there . pyc files that are compiled bytecodes?

1 answer

9


Python, like Java, works with a virtual machine. In the case of Java, it is mandatory to send to the virtual machine only the compiled code, therefore the code of the .class. In Python, the virtual machine itself works with the language compiler, so it accepts pure Python code .py as well as its bytecode version .pyc.

On the question of one language being called interpreted and another being called compiled: in both cases, compilation and interpretation occurs. In the case of Java, compilation occurs at a time prior to execution, in the call to javac to make the transformation .java -> .class. After the .class, there is the interpretation of bytecodes generated to run the program. I can make an analogy here with C and the processor of my x86 machine; the gcc will transform the C code into x86 machine language, so that the processor receives the binary and runs them.

In Python, the compilation process does not need to occur a priori, happening at a later time. Just like in Java and C, there is a code reading doing the grammar validation (syntax error detection) and the transformation into something easier to operationalize. The result of a successful build is a set of Python bytecodes equivalent to the original Python code. After this compilation process, the bytecode interpreter is processed, just as the JVM (Java Virtual Machine) does with the Java compiler, analogous to how the x86 processor does with the binary generated by the C file compilation.

To avoid going through the entire compilation process again, which requires a grammar validation and looks for syntactic errors, once Python code compilation is generated, it can be stored for future use. This is done for the sake of optimization. As long as there are no changes in your file .py original, the .pyc will be generated faithful to the desired code, behaving in an expected manner. At the moment you change the .py, there is the detection of this change and the Python compiler enters the field to generate the new .pyc.

In cases where it is not possible to generate the file .pyc (for example, Python code running from a read-only file system), this optimization does not occur, so it will always occur the compilation of the original code to then occur the interpretation and execution of its bytecodes.

  • 1

    cool answer!

  • @jsbueno , has any question/answer about files .pyo? I had thought that I had covered this in my reply, but reading again I realized that I did not.

  • 1

    Archives .pyo are almost the same thing as .pyc - but are generated when Python is called with the option -O of "Optimized" - in practice the compiler does not generate the code of some instructions that should be used only for debugging/testing like commands assert. The difference of this optimization is usually very small, so you don’t find many files .pyo thereabout.

  • 1

    In this answer perhaps it was worth completing that from Python 3.5 (or was it 3.4?), the files.pyc are now placed in the folder __pycache__ and with the compiler name and version integrated with the file name .pyc - this allows . py files in the same folder to be run by different versions of Python without conflict with pyc generated.

  • @jsbueno porei yes, thanks for the information!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.