Under Python compile py into PYc and PYO

Source: Internet
Author: User

under Python compile py into PYc and pyo

It's simple, actually.
Use
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 the job:
Code:


Import Py_compile
Py_compile.compile (' path ')//path is the path that includes the. py file name



Use
Python-o-M Py_compile file.py

Compiled into a pyo file.
1. The-M is equivalent to the import in the script, where the-M py_compile is equivalent to the import py_compile
2.-o If you change 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 file

What 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 This parameter English can not understand, please greatly enlighten us. (Despise oneself) Original: It is used as the name of the source file in error messages instead of file

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 English does not understand, the original: 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)


Summarize

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.

Here is the strange problem wizardpisces encountered: Just run the name.py program first, run successfully before entering import name, and then generate the Name.pyc file in the corresponding folder


Source: https://my.oschina.net/wizardpisces/blog/107445

Null

Under Python compile py into PYc and PYO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.