What is a PyC file?
PyC is a binary file generated after the py file is compiled. It is a bytecode file. After the py file is changed to a PyC file, the loading speed is improved, in addition, PyC is a cross-platform bytecode that is executed by a python virtual machine, which is similar to Java or. net Virtual Machine Concept. The content of PyC is related to the Python version. The Python files compiled by different versions are different. The Python files compiled by 2.5 cannot be executed.
Why is the PyC file required?
This requirement is too obvious, because the py file can directly see the source code. If you are developing commercial software, it is impossible to leak the source code? Therefore, you need to compile it into PyC and then release it. Of course, the PyC file can also be decompiled. different versions of the compiled PyC file are different. According to the opcode provided in the Python source code, The py file source code can be decompiled Based on the PyC file, you can find a tool to decompile the PyC file of python2.3 on the Internet. However, this tool is charged from python2.4. If you need to decompile the new version of PyC file, you Need To Do It Yourself (I don't have this capability yet ^ -- ^), but you can modify the opcode file in Python source code and re-compile python to prevent hacker cracking.
Generate a single PyC File
Python is a good thing. It provides a built-in class library to compile the py file into a PyC file. This module isPy_compile module.
The usage is very simple, as shown below. In idle, You can compile a py file as a PyC file. (In windows)
Importpy_compile
Py_compile.compile (r'h:/GAME/test. py ')
Compile function prototype:
Compile (file [, cfile [, dfile [, doraise])
File indicates the path of The py file to be compiled
Cfile indicates the name and path of the compiled PyC file. By default, C or O is directly added after the file name. O indicates the optimized bytecode.
The dfile parameter is not clear in English. Please give me some advice. (Despise yourself) Original article: it is used as the name of the sourcefile in error messages insteadofFile
Doraise can be set to either true or false. If it is set to true, a pycompileerror is triggered. Otherwise, an error occurs in the compilation file, which is displayed in SYS by default. stderr, without exception
(From python2.5)
Generate PyC files in batches
In general, our projects are all in a directory. Generally, we don't just compile a py file. Instead, we need to compile the py files in the entire folder into PyC files, python provides another module: compileall. The usage is as follows:
Importcompileall
Compileall.Compile_dir (r 'H:/game ')
In this way, the game directory and The py file under its subdirectory are compiled into a PyC file. Hey, it's convenient. Let's seeCompile_dirFunction Description:
Compile_dir (dir [, maxlevels [, ddir [, force [, RX [, quiet])
Dir indicates the folder location to be compiled
Maxlevels indicates the number of layers of sub-directories to be recursively compiled. The default value is 10. That is, The py file in the sub-directories of Layer 10 is compiled into PyC by default.
Ddir: It is usedas the base path from which the filenames used in error messageswill be generated.
If force is true, it is forcibly compiled to PyC. Even if the current PyC file is up-to-date, It is forcibly compiled once. The PyC file contains a timestamp, the Python compiler determines whether to generate a new PyC file based on the time.
RX indicates a regular expression. For example, you can exclude unwanted directories or compile only directories that meet the conditions.
If quiet is true, the information will not be printed in the standard output after compilation.