Actually very simple, with python-m py_compile file.py
Python-m py_compile/root/src/{file1,file2}.py compiled into a pyc file. You can also write a script to do this: Code:
Import py_compile py_compile.compile (' path ')//path is the path that includes the. py file name |
With Python-o-M py_compile file.py
Compiled into a pyo file. 1. The-m equivalent of the import in the script, where the-m py_compile equivalent to the above import Py_compile 2.-o if changed to-oo is to delete the corresponding pyo file, specific help can be entered in the console python-h view
========================
From:http://blogold.chinaunix.net/u3/93255/showart_1944929.html
What is a PYc file
PYC is a binary file, which is generated by the py file after compiling, is a byte code,py file becomes PYc file, the speed of loading is increased, and PYC is a cross-platform bytecode, is executed by Python virtual machine, This is the concept of a virtual machine similar to Java or. Net. PYC content, is related to Python version, the different version of the compiled PYc file is different, 2.5 compiled PYC files, 2.4 version of Python is not executable.
What is a pyo filePYO is an optimized compiled program Python-o source file to compile the source program into a pyo fileWhat is a PYD filePYD is a dynamic link library for Python. Why do I need PYC files
This demand is too obvious, because the py file can be directly see the source code, if you are developing commercial software, it is impossible to release the source code too? So it needs to be compiled into PYc and then released. Of course, PYC files can also be anti-compilation, different versions of the compiled PYC file is different, according to the python source code provides opcode, can be based on PYC file to decompile the Py file source code, Online can find an anti-compilation python2.3 version of the PYc file tool, but the tool from the beginning of python2.4 charge, if you need to decompile the new version of the PYc file, you need to do it yourself (I do not have this ability ^--^), However, you can modify the Python source code in the opcode file, recompile python, so as to prevent the criminals to crack.
generate a single PYC file
Python is a good thing, it provides a built-in class library to implement the Py file compiled into a PYC file, this module is the py_compile module.
The use of the method is very simple, as shown below, directly in the idle, you can compile a py file into a pyc file. (assuming a Windows environment)
Import Py_compile
Py_compile.compile (R ' H:\game\test.py ')
Compile function Prototypes:
Compile (file[, cfile[, dfile[, Doraise]])
File indicates the path of the py file that needs to be compiled
The CFile represents the compiled PYC file name and path, which by default is a byte code that adds C or o,o directly after the file file name
Dfile the file shown in the error message is replaced with Dfile
Doraise can be two values, true or FALSE, if true, a pycompileerror is thrown, or if there is an error in compiling the file, there will be a bug that appears by default in Sys.stderr without throwing an exception
(from python2.5 documentation)
Batch build PYC files
Generally speaking, our project is in a directory, generally do not say just compile a py file, but need to be the entire folder of the py file compiled into a pyc file, and Python for us to provide another module: Compileall. Here's how to use it:
Import Compileall
Compileall. Compile_dir (R ' H:\game ')
You can also compile files in a directory directly from the command line, such as: # Python-m compileall/root/src/
This compiles the game directory and the py file under its subdirectory into a PYC file. Hey, it's easy enough. Take a look at the description of the compile_dir function:
Compile_dir (dir[, maxlevels[, ddir[, force[, rx[, Quiet]] []]
Dir indicates the folder location that needs to be compiled
Maxlevels represents the number of levels of subdirectories that need to be compiled recursively, which by default is 10 layers, that is, by default, the PY files in the 10-tier subdirectory are compiled into PYC
Ddir is used as the base path for the file name that will produce the error message : It is used as the base path from which the filenames used in error messages would Be generated.
If force is true, it will be forced to compile to PYC, even if the current PYc file is up to date, it will be forced to compile once, the PYc file contains a timestamp, and the Python compiler will decide, depending on the time, whether the PYc file needs to be regenerated once
Rx represents a regular expression, such as the ability to exclude unwanted directories, or only those that are eligible for compilation
Quiet if true, the information is not printed in standard output after compilation
(from python2.5 documentation)
Summary
Through the above method, you can easily compile the py file into a pyc file, so that some of the source code can be hidden, to ensure that Python to do commercial software, to ensure that part of the security bar, continue to learn, see how to modify the opcode.
Under Python compile py into PYc and PYO