Cloud computing python Automation: Python file types explained

Source: Internet
Author: User

Python's file types are divided into 3 main categories: source code, byte code (byte-code file), and optimized bytecode (optimized file). The code can be run directly, without compiling or connecting. This is the characteristic of the Python language, and Python's files are run through Python.exe and Pythonw.exe interpretations. There are 3 types of files commonly used in Python:

    1. Source code PY

    2. BYTE code PYC

    3. Optimizing Code PYO

Source:

Python source files with "py" extension, interpreted by the Python program, do not need to compile

BYTE code:

The Python source file is compiled with a file extension of "PYC" that hides the code and increases the load-speed compilation method:

To generate a single PYC file:

Method 1: Code mode

#vim compile.py

Import Py_compile

Py_compile.compile ("hello.py")

Method 2: Command-line mode

#python-M Py_compile hello.py

-m equivalent to the import in the script

Batch Generate 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, Python also provides us with another module: Compileall. Here's how to use it:

Method 1: Code mode

Import Compileall

Compileall.compile_dir (R '/game ')

Method 2: Command-line mode

Python-m compileall/root/src/

Optimized code:

After the optimized compiled file, the extension "PYO" PYO is optimized compiled program, also can improve loading speed, for embedded system, the required modules compiled into PYO files can reduce capacity, essentially and PYC no difference

Optimization method:

#python-O-M py_compile hello.py

Note: Byte code and optimization code cannot be executed directly, only with the interpreter

Correct execution method:

#python HELLO.PYC

#python Hello.pyo

Error execution Method:

#./hello.pyc//This execution will error

Valgus:

    1. What is a PYc file: PYc is a binary file that is compiled by a py file, the resulting file is a byte code,py file that becomes a PYc file, the speed of loading is increased, and PYC is a cross-platform bytecode that is executed by Python's 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.

    2. Why need PYC file: This demand is very 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, You can find a tool to decompile the python2.3 version of the PYc file on the Web, but the tool will be charged from python2.4, and if you need to decompile a new version of the PYc file, you need to do it yourself. However, you can modify the Python source code in the opcode file, recompile python, so as to prevent the criminals to crack.

Cloud computing python Automation: Python file types explained

Related Article

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.