Application description of the specific solution for compiling Python source code

Source: Internet
Author: User

In computer languages, Python source code is generally not compiled, but can be used only through several files, the following articles mainly describe how to compile Python source code through Python source code.

We generally use this command to run a python program:

 
 
  1. C:\> python im.py 

The content of the im. py file is:

 
 
  1. #!/usr/bin/env python  
  2. import mymodule  
  3. mymodule.say(”hello”)   

After executing this command, the Python interpreter does not necessarily read the mymodule. py file. It will try to read the mymodule. pyc file or the mymodule. pyo file. If both failed, or the mymodule. py file is newer than mymodule. pyc/mymodule. pyo, The mymodule. py file will be read in turn.

Python only understands the execution of bytecode, so the first thing after reading the mymodule. py file is to compile it first. The compilation result is saved to the mymodule. pyc file. We will discuss how to control the compilation process of Python later.

Although similar to a static language such as C ++/Java, the term compile is used, the actual situation is still different. First, the most obvious point is that the final result of Python source code compilation is not a machine code, but a bytecode. Python compilation mainly involves grammar analysis, generating an abstract syntax tree, and dumping it into bytecode.

  • Import and from in python syntax
  • Introduction to the Python Chinese garbled Problem Analysis Solution
  • Application of the python programming language in the operating file encoding format
  • How to install and configure Python
  • Analysis of the actual application of the Python configuration file

From the above introduction, we can find that Python compilation is far from C ++. However, it is somewhat similar to Java because they all generate bytecode. Java compilation is much more advanced than Python. When executing Java bytecode, the Java interpreter will use JIT to convert hotspot such as cyclic operations into machine code. So sometimes Java performance can reach the level close to C ++.

The lack of JIT in Python is not intentional, but lacks human resources. There is already an external module of pysco, which is said to greatly improve the speed of Python code, but this module has not yet entered the official Python code.

The above article describes the actual operation steps for compiling Python source code.

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.