python-- packet module

0 Preface

 

General packet import mode is performed by the following statement:

Import dir1.dir2.mod
 # . represents a spacer path corresponding to / under the Linux 
# mod means of this path is called a file mod.py, and are omitted here .py

 

 

  However, the use package imports must comply with a constraint: there must be a path within the package __init__.py import statement in each directory , that dir1, dir2 must have a __init__.py in this document.

For more than such import statement, you must observe the following rules:

  • dir1 and dir2 must be included in a file __init__.py
  • dir0 is a container, no __init__.py file, if any, this file will be ignored.
  • dir0 (rather than dir0 \ dir1) must be listed on the module search path (that is, the directory must be the main catalog, or a column in PATHONPATH)

Provided in Pycharm is performed by setting the flag to a root File-> Settings-> project structure as well as the containers Mark Source (source), excluded (negative) of.

 

1.__init__.py

__init__.py can contain Python code, just like a normal module files. Such a declarative document speaks as of Python to some extent, however, it can also be completely empty. As a statement, these files can prevent the directory with the same name carelessly hidden in the module search path, and after the modules really need to appear.

__init__.py file plays a package initialization hook, generating module for directory and namespace achieve from * (from ... import *) role behavior when using directory import. Packages may use its initialization file (__init__.py) to create a data file required, connection database.

 

from * statement behavior: as an advanced feature, you can use __all__ list in the __init__.py file to define the directory to import from * statement form, what needs to be imported.

__All__ __init__.py file in the list is the name of the list of sub-module when the package (directory) name from * to use, it should be imported. If no __all __, from * statement does not automatically load submodules nested within that directory. Instead, load only the directory __init__.py file assignment statement defines the variable name. Including the file into the program code is clear of any sub-module.

 

2. Relative introduced

from the previous statement you can now use the dot ( ".") to specify that they need to be in the same module package (the so-called package relative to import ), rather than in the module search path somewhere on the import module ( called absolute import ) .

Such as a folder exists __init__.py, a.py, b.py, if it wants to import of a module b.py.

Import b # is not recommended, if b is the same as the name of the module does not exist, you will eventually play the same effect following 
from . Import b # recommend proper wording which is relatively imported, but also to force the relative import 
from .a Import var1 # imported from the module named a variable var

 

 

But first relative and then absolute default search order in python2.x, whereas python3.x is the first absolute then relative search order. In order to maintain consistency and code python2 (ie written in python2 also compatible code) python3 in, you should add the following sentence:

from __future__ import absolute_import

 

 Introducing relative scope:

  • Introducing relative introduced only applies to the bag.
  • Only for import from opposite statements. And have one or more points from a number of preceding module name.

 

Module looks rules summary:

  • Simple module name (eg, A) to look through each directory on the search path sys.path list, from left to right. The list of default by the system settings and user configuration settings components.
  • Package is the direct directory pychon modules with a special __init__.py file. This introduction may be used such that one ABC directory path syntax. In an ABC introduced, a directory named A is positioned relative to conventional search module sys.path introduced, B is a subdirectory of another package of the A, C or B is a module may be introduced in other items.
  • In a package file, and use conventional import statement to import the rest of the same sys.path search rules. From the package into the use of the dot and the foregoing statement, however, it is relative to the packet; in other words, only check the package directory, and do not use conventional sys.path lookup. For example, from. Import A, the module limit the search to include the files in the directory that appears in the statement.

 

Finally, a question: Under what circumstances must not pass from the package through the use of import?

You only need to read the definition in the same variable name more than one path, it must import to use package, but can not be used from. Use import, you can make the path of a unique reference, however, from any variable name allows only one version.

 

Guess you like

Origin www.cnblogs.com/SsoZhNO-1/p/11402452.html