Research on the Python Module

Source: Internet
Author: User

To increase the startup time of small programs that call many standard Python modules, an important measure is to find the "spam. there is a directory named "spam. the pyc file contains a so-called "Byte compilation" version of the module spam.

Used to generate "spam. "spam. the modification time of py is recorded in "spam. in pyc, if the modification time of the record does not match the current file time, the compilation file will be ignored. Generally, you do not need to generate a compilation file such as "spam. pyc. Every time "spam. py" is compiled successfully, the interpreter tries to write the compiled version "spam. pyc". If it cannot be written, no error will be made;

  • Analysis of the entire process of parsing Python parser
  • In-depth analysis of Chinese Python garbled characters
  • How can I install Python correctly?
  • Python program description
  • Introduction to Python control statements

If the file is not written for some reason, the generated "spam. pyc" is identified as incomplete and ignored. The format of the compiled file "spam. pyc" does not depend on the platform. Therefore, machines with different structures can share the Python Module Directory.

The following are tips for experts:

If the Python interpreter is started with the-O flag, the optimized compilation code is generated and saved in the ". pyo" file. Currently, there are not many optimizations. Now we just remove the assert statement and the SET_LINENO command. When the-O flag is used, all bytecode is optimized, the ". pyc" file is ignored, and the ". py" file is compiled as the optimized bytecode. Explain the optimization code generated by the two optimization flags-OO) to Python sometimes causes the program to run abnormally.

Currently, dual optimization only deletes the _ doc _ string from the bytecode, making the ". pyo" file small. Some programs may rely on document strings, so such optimization can be used only when you are sure there is no problem. Programs read from ". pyc" or ". pyo" cannot run faster than those read from ". py", but they only call faster. If a program is run by specifying the script file name through the command line.

The script bytecode is not written to the ". pyc" or ". pyo" file. Therefore, if the main code of the program is moved into a module, only the pilot program imported into the module is left in the script, the script startup time can be slightly shortened. There can be a file called "spam. pyc" when the-O flag is used as "spam. pyo") without the corresponding source file "spam. py ". This can be used to distribute a Python code library that is difficult to decompile.

The module compileall can compile all modules in a directory as the ". pyc" file and specify the-O option as the ". pyo" file ). Python comes with a standard module library, which is described in another document "Python Library Reference.

Some modules are directly incorporated into the interpreter. These modules are not the core of the language. They are incorporated into the interpreter to improve the running efficiency or to provide the underlying functions of the system to call such a system. Provides the compilation options for those modules.

In Python, you can use a "package" to organize the Python module namespace. You can use a "module name with vertices" for name reference. For example, Module name A. B indicates the sub-module named "B" in package ". Just as using modules, the authors of different modules do not have to worry about conflicting global variable names, using the module name with dots can enable the authors of multi-module packages, such as NumPy and PIL, not to worry about conflicting modules.

For example, the Python module can only be provided in systems that provide the amoeba underlying commands. One module deserves special attention: sys module, which is compiled into every Python interpreter. The sys. ps1 and sys. ps2 variables define the initial and continued prompts during the interactive operation.

These two variables are defined only when the interpreter runs interactively. The sys. path variable is a string list that determines the module search path of the interpreter. It is initialized to the default path specified by the Environment Variable PYTHONPATH. When the environment variable is not defined, it is initialized to the default path during installation. You can use the standard list operation to modify the search path. For example:

 
 
  1. >>> import fibo, sys  
  2. >>> dir(fibo)  
  3. ['__name__', 'fib', 'fib2']  
  4. >>> dir(sys)  
  5. ['__name__', 'argv', 'builtin_module_names', 'copyright', 'exit',  
  6. 'maxint', 'modules', 'path', 'ps1', 'ps2', 'setprofile', 'settrace',  
  7. 'stderr', 'stdin', 'stdout', 'version']  

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.