python syntax base (8) _ Package

Introduction package

Official website explains
Packages are a way of structuring Python's module namespace by using "dotted module names"
package is a way to organize python module namespace by using a 'module name'.
Specifically: a package that contains the __init__.py file folder, so in fact our purpose is to create a package with the file folder / modules organized
needs to be emphasized:
  1. In python3 in, even if no package __init__.py file, import the package is still not being given, but in python2, the next package must have the file, or import the package error
  2. create the purpose of the package is not to run, but was introduced into use, remember, package module is essentially just a form of it, is a modular package

Use package

The nature of the package is a folder, the folder only function is to organize your files together
with the function write more and more, so we could not function are placed in a file, so we use the module to organize functions, and as more and more modules, we need to use the module file folders to organize themselves in order to improve the structural and maintainability program

Precautions

1. Import-related statements about the package is also divided into import and from ... import ... are two, but no matter what, no matter what position, when you import must follow a principle: when importing a little all the the left point must be a package, otherwise illegal. It may have a series of points, such as item.subitem.subsubitem, but must follow this principle. But after the introduction, when using no such limit, the package may be left point, modules, functions, classes (which can be called its own properties manner points).
2, when the import file import, generate namespace name from file, import the package, the name of the name space generated from the same document, i.e. __init__.py the packet, the packet is essentially introduced in the import file
3, the module does not have the same name conflict and the packets A packet B, Aa and Ba as from two namespaces

Importing absolute and relative import

Absolute introduced: as starting glance at
opposite introduced:. .. manner or with most start (only in a package, can not be used in different directories)
packet, and a module included in the packet is intended to be introduced rather than being directly executed. The environment variables are subject to execution of a file

Absolute and relative import import summary

Absolute introduced: to perform file is sys.path import start point, called the absolute introduced
advantages: execution file and the imported module can use
disadvantage: In all sys.path are introduced as a starting point, problems introduced

Relative import: reference to a current location of the folder starts searching, called relative import
symbol: represents the current file plus the file resides, .., on behalf of a folder, ... on a representative of the a folder
advantages: easier introduction
disadvantages: only be used during import module package
Note:
     1. import only relative to each other between the introduction means for the inner bag, is introduced by the importer must be present in a package
     2. attempted relative import beyond top-level package # attempt to use relative imports are wrong, meaning, you must use relative imports of a premium package in addition to the top-level package, each additional representative to jump to the previous file clip, while the upper level should not exceed the top package

Guess you like

Origin www.cnblogs.com/SkyRabbit/p/11317608.html