Python Project---data visualization (02)

Source: Internet
Author: User

When writing a program today, an interesting phenomenon was found. When the import statement is executed, a __pycache__ file is generated in the script directory after it is run . so I made the following summary explanation:

I. Python basic operating mechanism

Python programs run without the need to compile into binary code, and directly from the source to run the program, in short, the Python interpreter will convert the source code to bytecode, and then by the interpreter to execute the bytecode.

The specific work of the interpreter:

1 Complete the module loading and linking;

2 Compile the source code into a Pycodeobject object (that is, bytecode), write in memory for the CPU to read;

3 reads and executes from memory and writes Pycodeobject back to the hard disk, which is copied to the. pyc or. pyo file to save the bytecode file for all scripts in the current directory;

* After the script is executed again, it checks whether the "locally-available bytecode file" and "The bytecode file's modification time are consistent with its script". is directly executed, otherwise repeat the above steps.

Two. Why does the file appear

The Python interpreter compiles the *.py script file and saves the compilation results to the __pycache__ directory.

The next time the project is executed, if the interpreter finds that the *.py script has not been modified, it skips the compile step and runs the previously generated *.PYC file saved in the __pycache__ folder.

This can greatly shorten the preparation time of the project before it is large; if you only need to perform a small project, it's OK to ignore this folder.

Three. How to make the file not appear

1. Single shutdown: Add parameters when running scripts -B

2. Permanently close: Set environment variables to PYTHONDONTWRITEBYTECODE=1

Four. Explanation of the contents of the document

In this file, there is a similar format RANDOM_WALK.CPYTHON-37.PYC file with varying sizes, and the first digit in the dot number represents the template that was inport in. cpython-37, cpython represents the C language implementation of the Python interpreter, which -37 represents version 3.7.

Five. Module invocation

When importing a module in Python, it actually executes the imported module once, as follows:
First look at the module being called test.py :

def haha():    print("哈哈")haha()

Look at the main program again main.py :

import testprint("一条鱼")

The execution results are:

哈哈一条鱼

How can you simply invoke the code without executing the called module? To be called module code is not executed, the premise is to know __name__ what the variable means, in short, if not involved in the module import, __name__ The value is " __main__ ", if the module is imported, then __name__ the module in the The value is the name of the file (without the. py), as follows test_1.py :

def haha():    print("哈哈")haha()print(__name__)

test_1.pyThe result of the execution is:

哈哈__main__

If you test_1 are importing references, such as test_2 :

import test_1print("一条鱼")

test_2X Run result is:

哈哈test_1一条鱼

If we can understand the above, then in the module that is called, the executable code is preceded by the judgment that the if __name__ == ‘__main__‘: code of the called module will not be executed! Or, add the option parameter when you start it again. This file can be deleted, but the deletion is of little significance. Instead, the load will be faster.

Note:pyo and. pyc files do not execute faster than. py files, and the speed at which the modules are loaded is fast.

Reference Test Literature:

79023987

Https://www.cnblogs.com/Ralph-Wang/archive/2013/11/23/3439080.html

Https://jingyan.baidu.com/article/c14654134a7bdd0bfcfc4cea.html

Python Project---data visualization (02)

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.