Is python an explanatory language? Until you see a python py, PYc, PYO, PYD file

Source: Internet
Author: User
Tags function prototype

PY is the source file, PYc is the source file compiled file, Pyo is the source file optimization compiled file, PYD is written in other languages of the Python library

1. Is python an interpreted language?

Python is an explanatory language, and I've been believing it until I find out the existence of the *.pyc file.

If it is an interpreted language, what is the generated *.pyc file? c should be the abbreviation of compiled!

In order to prevent other people who learn python from being misunderstood by this remark, we will clarify this issue in the text and make some basic concepts clear.

Python is not entirely an explanatory language, it is compiled, the source py file is compiled into PYC or PYO, and then executed by the Python virtual machine, compared to the Py file, compiled into PYC and Pyo essentially and PY is not much different, Just for this module loading speed increased, and did not improve the execution speed of the code, usually do not have to actively compile the PYc file, the document said that as long as the import model is called model.py will be compiled into PYC and then loaded

2. Explanatory and compiled languages

Computers are not able to recognize high-level languages, so when we run a high-level language program, we need a "translator" to engage in the process of translating high-level languages into machine languages that computers can read. This process is divided into two categories, the first of which is compilation, and the second is interpretation.

A compiled language before a program executes, the program executes a compilation process through the compiler, transforming the program into machine language. The runtime does not need to be translated and executes directly. The most typical example is the C language.

The explanatory language does not have this process of compiling, but rather, when the program is running, it interprets the program line by row, then runs directly, and the most typical example is Ruby.

Through the above example, we can summarize the advantages and disadvantages of the explanatory language and the compiled language, because the compiler language before the program has already made a "translation" of the program, so at run time there is less "translation" process, so the efficiency is higher. However, we cannot generalize that some explanatory language can also be optimized by the interpreter to optimize the whole program when translating the program, so that it is close to the compiled language in an efficient and not more than a compiled language.

In addition, with the rise of virtual machine-based languages such as Java, we cannot simply divide the language into two types-----explanatory and compiled.

In Java, for example, Java is first compiled into a bytecode file by a compiler and then interpreted as a machine file by the interpreter at run time. So we say that Java is a language that is compiled and interpreted first.

3. What exactly is Python?

In fact, Python, like java/c#, is also a virtual machine-based language, let's start with a simple look at the Python program's running process.

When we enter Python hello.py on the command line, we actually activate the Python interpreter and tell the interpreter: You're going to start working. But before the "explain", the first thing that actually executes is the same as Java, which is compiled.

Students familiar with Java can consider how we execute a Java program on the command line:

Javac Hello.java

Java Hello

Just when we were using an IDE like Eclipse, we fused these two parts into a piece. In fact, Python is also the same, when we execute Python hello.py, he also executes such a process, so we should describe the Python,python is a first compiled after the interpretation of the language.

4. Brief description of Python's running process

Before we say this question, let's start with two concepts, pycodeobject and PYC files.

The PYC we see on the hard drive naturally doesn't have to say much, and pycodeobject is actually the result of a Python compiler actually compiling it. Let's just get to the bottom of it and keep looking down.

When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

When the Python program runs for the second time, the program will first look for the PYc file on the hard drive, if found, Determine the last modification time of the. pyc file and the. py file, if the. pyc file is modified later than the. py file, indicating that the source code in the. py file has not been modified, and is directly loaded, otherwise the above procedure is repeated.

So we should be able to locate Pycodeobject and pyc files, we say that PYc file is actually a kind of persistent saving way of pycodeobject.

5. TodayUnder Python compile py into PYc and PYO

(1) It's actually very simple,

    • Compiling into a *.PDC file

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'//pathis the path that includes the. py file name 

    • Compiled into a pyo file.

Python-o-M Py_compile file.py

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

    • Compiled into a PYD file.

PYD is not generated from a Python program, but an extension written in other languages that can be called by Python.

Can view this article: http://www.cnblogs.com/nucdy/p/7736155.html

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_compilepy_compile.compile (R'H:\game\test.py') Compile function prototype: 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 offile

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 Compileallcompileall.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]] []]

 is  as Base  from inch error messages'll be generated. D, force if true, will be forced to compile to PYC, even if the current PYc file is up-to-date, will be forced to compile once, PYc file contains a timestamp, the Python compiler depends on the time to determine whether the need to regenerate PYc file E, Rx represents a regular expression, For example, you can exclude the unwanted directory, or only the qualified directory to compile F, quiet if true, after compilation, will not be printed in the standard output, the information

From the Internet: http://www.cnblogs.com/zoe233/p/6993972.html, etc.

Is python an explanatory language? Until you see a python py, PYc, PYO, PYD file

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.