What Is Py Compile?
Programmers use the Python programming language to write flexible scripts for desktop applications and Web software. Python's ease of use come from the fact that it is a high-level interpreted language that includes many built-in functions and data structures "out of the box." However, Python might not execute as quickly as other compiled languages such as C/C++ or Java. To aid execution speed, the Python interpreter contains functionality in the "py_compile" module in order to pre-compile modules for use.
-
Interpreted Languages
-
Python is an "interpreted" language. Unlike compiled languages such as C/C++ or Java, Python is not translated into a binary file before execution. C/C++ and Java exist as binary files that the computer executes as machine code. Python, as an interpreted language, runs on an interpreter that reads and executes each line of code one at time. The interpreter runs the machine code of the Python source code. The interpretation layer of the code slows code execution, but allows dynamic features not usually incorporated into compiled languages, such as weak typing of variables.
Python and Compilation
-
One of Python's strengths is its library, on included modules that automate basic programming tasks. When these modules are imported into a file, the interpreter has to read them the same way the source code is read. For example, a Python script that imports the "sys" module will force the interpreter to read the sys module code first, which can be extensive, and then finally read the actual program's code. This will have to happen each time the file is run. However, Python attempts to circumvent this problem by compiling certain files.
-
Python Compiled Files
-
When modules are loaded into a program in a programmer's Python environment, the interpreter compiles the module into a pre-interpreted file called a ".pyc" file. This way, when the programmer uses the module in other programs, the Python interpreter will refer to its pre-interpreted form rather than executing the module over and over again. This compilation process shortens execution time, by allowing often-used modules that rarely change to only undergo one interpretative execution.
Py_compile
-
The "py_compile" module offers this functionality for general modules the programmer might want to compile from Python source code. By using the py_compile module, the programmer can compile his own source code into Python byte code. Not only does this aid in speeding up execution for choice parts of the Python program, it also allows various programmers to share identical Python libraries across their different programming environments.
-