Python: Module, pythonmodule

Source: Internet
Author: User
Tags builtin

Python: Module, pythonmodule

In Python, A. py file represents a Module. In the Module, it can be any Python script that complies with the Python file format. It is useful to understand the Module import mechanism.

  • 1 Module composition
    • 1.1 Module built-in global variables
  • 2 Module Import
    • 2.1 import and use
    • 2.2 load multiple imports at a time
    • 2.3 search path
    • 2.4 reload
  • 3 Package
    • 3.1 _ init _. py
    • 3.2 _ all __
    • 3.3 _ path __

 

1 Module composition

A. py file is a module. Module includes attribute and function. The attribute mentioned here is actually the global variable of the module.

In a ModuleTests. py file:

#! Python #-*-coding: UTF-8-*-"global variable" # hello docglobal moduleNamemoduleName = _ name _ a = 1def printModuleName (): print (a + 1) print (_ name _) print (moduleName) ''' if _ name _ = '_ main __': print ('current module name is "'+ _ name _ +'" ') ''' printModuleName () print (a) print (dir ()) import _ builtin _ print (_ builtin _ = _ builtins _) print (_ doc _) print (_ file __) print (_ name _) print (_ package _) _ name _ = 'hello' print (_ name __)View Code

In addition to the global variables and functions you define, each module also has some built-in global variables. The module contains three attributes: a, moduleName, and printModuleName. If this module is imported to another module, you can access these three attributes in some way.

 

1.1 Module built-in global variables

Each module has some default attributes (global variables ). The dir () function is a top-level function in python and has the courage to view the module content. For example, in the preceding example, you can use dir () to view the result:

['_ Builtins _', '_ doc _', '_ file _', '_ name __', '_ package _', 'A', 'modulename', 'printmodulename']. A, moduleName, and printModuleName are customized by the user. Others are built-in.

1)_ Name __: The name of the module. For example, in ModuleTests. py, the module name is ModuleTests by default. During running, if a module is a program entry, _ name _ is "_ main __". It is the most commonly used.

2)_ Builtins __: There is a built-in module in Python called :__ builtin __, which is a Python module. Every Python module has a _ builtins _ global variable, which is a reference of the built-in module _ builtin. You can test it by using the following code:

import __builtin__print(__builtin__ == __builtins__)

// The test result is True.

3)_ Doc __: Module documentation. Even beginners of Python know that multi-line comments in Python are enclosed by three pairs of single quotes or double quotes. Some people on the Internet say that _ doc _ is actually a comment. This sentence is too casual and easy to misunderstand. After testing, confirm that _ doc _ should be:After the file header,Code (includingImport) BeforeOfFirstMulti-line comment.

4)_ File __: Path of the file where the current module is located.

5)_ Package __: The package name of the current module. If not, the value is None.

 

2 Module import 2.1 import and use

A Module can be imported to other Python scripts. There are multiple import methods:

1) import module1

2) import module1 as m1

3) from module1 import xxx

4) from module1 import xxx as yyy

Package import can be divided into three types:

1) import p1.p2. p3.module1

2) import p1.p2. p3.module1 as m1

3) from p1.p2. p3.module1 import xxx

4) from p1.p2. p3.module1 import xxx as yyy

Assume that module1 has two attribue: a1, a2, and two functions: f1 and f2, which indicate the differences between these import methods:

The first method is to import the entire module1 and assign the value to the module1 variable for use. You can use module1.a1, module1.a2, module1.f1 (params), module. f2 (params)

The second method is to rename method 1 to m1, that is, to use m1.a1, m1.a2, m1.f1, and m1.f2.

Method 3: import part of the module (import one or more attributes or functions ). For example, from module1 import a1. After the import is complete, an a1 variable is created in the current module. You can call a1 directly.

Method 4: You can rename an attribute or function when importing it. For example, after importing from module1 import a1 as msg, a msg variable is created in the current module, pointing to module1.a1. We can only use msg for calling.

2.2 load multiple imports at a time

 

For the above four import methods, either of them has two stages: 1) Find the module object, 2) assign to the variable on demand.

The module itself is used for reuse. In a large project, some basic and public modules are usually used in large quantities, that is, they are imported and used by many modules. We also know that the module is placed in the py file. If a module is imported in large quantities, do I need to find a py file on the disk for each import?

Obviously, you cannot design it like this. If you design it like this, the performance of the program will be very poor.

For the same problem, the method in Java is to use ClassLoader to load classes and use the parent loader delegation mechanism. Make sure that, in the same ClassLoader, the same class is referenced multiple times. This method can be called one-time loading and used in multiple places.

Python designers also consider this issue. A similar solution is also adopted, which is called one-time loading and multiple imports. Let's assume that it has a Module Loader. When it is loaded for the first time (in fact, it is imported for the first time), the execution process is as follows:

1) Module Loader finds the corresponding Module from the retrieval path

2) compile or find the appropriate bytecode file (ending with. pyc)

3) Explain and execute the Module to be imported and put it into the cache.

4) Allocate the imported Module object (or its attributes) to the variables under the current Module.

Then, when you execute the import moudle in the entire program, you only need to get the module from the cache and then execute 4 ).

 

In addition, for process 2) there are four situations:

A: If both. py and. pyc exist, the last modification time of the. py file is compared with the last modification time of the. pyc file. The execution time that is later than the execution time.

B: If neither. py nor. pyc exists, continue searching. If not, an error occurs.

C: If. py exists,. pyc does not exist: Compile. py as. pyc.

D: If. py does not exist and. pyc exists, run. pyc directly.

 

In addition, we also need to explain two points:

1) the one-time loading and multiple-time import mechanism does not work in the interactive command line provided in the Python package. In interactive mode, a single load can only be used for one import.

2) Generally, main py is not compiled into pyc. To compile a module into pyc, You need to import it to other modules.

 

2.3 search path

Based on Java programming experience, files are usually stored in different places. Python is no exception. The search sequence of Python is as follows:

1) cache of loaded modules

2) built-in modules

3) sys. path

Sys. path contains the following parts:

1) Directory of the Entry Program

2) Directory represented by the system environment variable PYTHONPATH

3) standard Python library directory

4) contents of any. pth file (if any)

Run the following command to check the directories in sys. path:

['','C:\\windows\\SYSTEM32\\python27.zip','D:\\Program Files\\Python\\Python27\\DLLs','D:\\Program Files\\Python\\Python27\\lib','D:\\Program Files\\Python\\Python27\\lib\\plat-win','D:\\Program Files\\Python\\Python27\\lib\\lib-tk','D:\\Program Files\\Python\\Python27','D:\\Program Files\\Python\\Python27\\lib\\site-packages']

If the module to be loaded is not in the preceding directory, you can use three minutes:

1) configure the environment variable PYTHONPATH in the same style as the environment variable PATH.

2) The program dynamically modifies sys. path

3) place it in the site-packages directory.

 

2.4 reload ()

In some cases, we need to modify the program code when the program runs. For example, we need to monitor the execution speed of a method and whether there are performance problems. We need to record a startTime and endTime In the start and end parts of the function to determine the execution performance accordingly. In such a scenario, because of how long the Module is loaded and called, the modified Code will not take effect. In this case, a mechanism is required to reload the module to achieve the desired effect. Reload () can solve this problem.

Reload (module) is a function and a parameter is a module object. Executing reload will be equal to loading again.

 

3 Package

 

3.1 _ init _. py

Each package must have a _ init _. py file, which indicates that the current directory can be used as a package.

_ Init _. py is also a python. When the corresponding package is loaded for the first time, _ init _. py is executed.

The _ init _. py file can have nothing, or you can specify _ all _ or (and) _ path.

 

3.2 _ all __

The value of _ all _ is a list, used when the program uses from pkg1.pkg2. pkg3 import.

Take the json package under Python_HOME/Lib/for experiment. As this file is large, I will write the main part:

__version__ = '2.0.9'__all__ = [    'dump', 'dumps', 'load', 'loads',    'JSONDecoder', 'JSONEncoder',]__author__ = 'Bob Ippolito <bob@redivi.com>'from .decoder import JSONDecoderfrom .encoder import JSONEncoderdef dump(params):    passdef dumps(params):    passdef load(params):    passdef loads(params):    pass

The directory structure is as follows:

 

When the from json import * is used in the program to cause the first loading of the json package, the execution process is as follows:

1) Find the json directory and execute _ init _. py. After execution is completed, the following files will be exposed: load, loads, dump. dumps, JSONDecoder, and JSONEncoder.

2) find *, that is, find the variable to be exported from _ all.

 

When the program uses Import json. encoder to load the json package for the first time, the execution process is as follows:

1) locate the json directory and execute _ init __. after py is executed, the following files are exposed: load, loads, and dump. dumps, JSONDecoder, JSONEncoder (for use in from json import)

2) import the json package to a variable (after that, the program can directly use json. encoder, json. decoder, json. Handler)

 

The above conclusions come from the following test cases:

#!python#-*- coding: utf-8 -*-"""Package Import Test"""#from json import *from json import encoderimport jsonprint(json.encoder == encoder)print(json.decoder is None)print(json.scanner is None)print(dir())

 

3.3 _ path __

This variable is used to configure the search location under the package. For example:

Add two directories for Linux and Windows under Utils, and each has an echo. py file. The directory is as follows:

Sound/Utils/| -- No _ init _ in the Linux directory __. py file, not a package, but a common directory | '-- echo. py | -- Windows directory does not contain _ init __. py file, not a package, but a common directory | '-- echo. py | -- _ init __. py | -- echo. py | -- reverse. py' -- surround. py

If _ init _. py is empty, Sound/Utils/echo. py will be imported when echo is imported using import Sound. Utils. echo. py.

Next, modify _ init _. py as follows:

import sys import os print "Sound.Utils.__init__.__path__ before change:", __path__ dirname = __path__[0] if sys.platform[0:5] == 'linux':         __path__.insert( 0, os.path.join(dirname, 'Linux') ) else:         __path__.insert( 0, os.path.join(dirname, 'Windows') ) print "Sound.Utils.__init__.__path__ AFTER change:", __path__

If you run import Sound. Utils. echo on Linux, the search path will change to 'sound/Utils/Linux 'and 'sound/Utils '. In order to achieve automated On-Demand Loading of response module functions.

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.