Python form ... ... the difference between import and import of (their understanding of)

There are two packages and introduction Python module manner, the following differences

form...import... import
Relative to the guide packet Introducing different modules buns
You can be introduced into the desired variables / functions / class module  
init file __all__ special variables (Module List)  
   
   
   

 

The import statement
import_stmt     ::=  "import" module ["as" identifier] ("," module ["as" identifier])*
                     | "from" relative_module "import" identifier ["as" identifier]
                     ("," identifier ["as" identifier])*
                     | "from" relative_module "import" "(" identifier ["as" identifier]
                     ("," identifier ["as" identifier])* [","] ")"
                     | "from" module "import" "*"
module          ::=  (identifier ".")* identifier
relative_module ::=  "."* module | "."+
The basic import statement (without the from clause) will be executed in two steps:

Find a module, if necessary will load and initialize the module.

Define the location scope for the import statement which occurred in the local namespace one or more names.

When the statement includes a plurality of clauses (separated by a comma) These two steps will be performed separately for each clause, as if they are divided into separate clauses as import statements.

I.e., a first step before to find and load modules into the system in a more detailed description, which also describes a number of types of packages and modules may be imported into the system and can be used to customize all the hooks objects. Please note that if this step fails, it may indicate that the module can not be found, or at initialization module, including the error occurred during the execution of the code module.

If the request is successfully acquired module, it may be used in one of the local name space of three ways:

If after with as module name, the name will be as follows the direct binding to the module imported.

If you do not specify a different name, and the name of the module to be introduced as the highest-level module, the module will be bound to the local namespace as a reference to the imported module.

If the name of the highest level of the package is introduced into the module is not the highest-level modules, including the module will then be bound to a local namespace to the highest level as a reference packet. The imported modules must use the qualified name to access its full and not directly accessible.

From the process used in the form of a slightly complicated some of them:

Find clause from the specified module, if necessary, will load and initialize the module;

For each identifier specified in clause import:

Check whether the module has been introduced into the attribute of the name

If not, try to import submodule having that name, check again whether the module has been introduced into the property

If the property is not found, caused ImportError.

Otherwise, the reference value is stored in the local namespace, as if there is a clause which specifies the name is used, otherwise the name of the property

E.g:

import foo                 # foo imported and bound locally
import foo.bar.baz         # foo.bar.baz imported, foo bound locally
import foo.bar.baz as fbb  # foo.bar.baz imported and bound as fbb
from foo.bar import baz    # foo.bar.baz imported and bound as baz
from foo import attr       # foo imported and foo.attr bound as attr
If the list of identifiers instead an asterisk ( '*'), all public names defined in the module are located will import the scope statement is bound to the local namespace.

A module public name is defined by the namespace variable called detection module to determine __all__; if defined, it must be a list of strings, wherein the entry for module defined or introduced name. In __all__ given in the name of the public it will be considered and should exist. If __all__ is not defined, the public name of the collection will not contain all the underscore character ( '_') found in the name starts with the namespace of the module. __all__ should include the entire public API. Its goal is to avoid accidental key derived (e.g. introduced and used in the module library module) is not part of the API.

Introduced in the form of wildcard --- from module import * --- it is only permitted on the module level. Try to use it throws SyntaxError class or function definition.

When you specify which module to import, you absolutely do not have to specify the name of the module. When a packet is contained in the module or in another package, it may be introduced in the same relative to the highest level in the package, not to mention the package name. By using a prefix number specified point after from a module or package, you can specify the number of the current package hierarchy level to be traced without specifying the exact name. Prefix represents a dot is to perform the current packet module introduced where the two dots indicates a packet level traced. Three dots indicate traced back two, and so on. So if you run from. Import of a location module within pkg package, you will eventually introduced pkg.mod when mod. If you run from ..subpkg2 import mod to pkg.subpkg1 location when you import the pkg.subpkg2.mod. For relatively imported specification included in the package in an opposite introduced.

importlib.import_module () is provided to determine the application module to be imported to support dynamically.

Trigger an audit event import incidental parameter module, filename, sys.path, sys.meta_path, sys.path_hooks.

 

 

 

The official document looks around did not directly say the difference between the two guide package, saying it touches the different uses of lead when handled in a different package, and finally read or baffled. . .

Guess you like

Origin www.cnblogs.com/XingXiaoMeng/p/12134317.html