What Are "PYC" Files?
The Python programming language is noted for its straightforward simplicity and portability. Part of this comes from its foundation as an interpreted scripting language. However, calling Python an entirely interpreted language would be misleading. In fact, Python, much like Java, works with the concept of "bytecode" to facilitate its execution. Special Python files called ".pyc" files represent "compiled" Python code that facilitate quick execution and program portability.
-
Interpreted Versus Compiled Languages
-
Traditionally, computer programming languages were usually "compiled," or reduced to machine instructions, before the computer could execute a written program. These programs ran on the native hardware with very little middleman software. Interpreted languages, on the other hand, run on an "interpreter." An interpreter takes source code and executes it line by line. This means that while compiled languages run faster, interpreted languages have more flexibility as to what commands and syntax they can incorporate into their instruction set.
Bytecode
-
Somewhere in the middle of these two paradigms lies the concept of portable compiled languages. The Java programming language represents one of the first of this type of code. Java source code is compiled into Java "bytecode." Java bytecode is then interpreted by a Java Virtual Machine into machine specific instructions. This means that bytecode languages such as Java compile into executable files like compiled languages, but the bytecode programs can run on any JVM existing on any platform. This makes programs written in Java as portable as those written in interpreted languages, while still not running as fast as pure compiled languages.
-
Python Bytecode
-
Programmers might often describe Python as in interpreted language. However, Python source code, like Java source code, also breaks down into bytecode. When a programmer writes a Python program, the source code gets translated to Python bytecode, and the Python interpreter then interprets the bytecode line by line during execution. Files of Python bytecode ends with the ".pyc" extension. Utilities exist to pre-compile Python code, and modules imported into a script are automatically compiled for later use.
Uses for ".PYC" Files
-
Modules that are imported into user scripts get compiled by the interpreter before execution. Because these modules tend to undergo repeated use, the interpreter compiles the module and stores the ".pyc" file in a directory. This way, when a script imports that module, the bytecode version already exists, ready for use. Furthermore, bytecode ".pyc" files are portable across multiple platforms, making pre-compiling Python scripts useful for distributing Python programs across different operating systems.
-