The package python

package

What is the package?

Package is a form of a module, the essence is a packet containing the file of the folder __init__.py

Why should pack?

The first version of the module is only 10 function, but in the future when the extended version, module name and usage should be best not to change, but this is only for user-friendly, and because the extended version, files increases, modular design by management module, maintenance will be more complicated, so we can use the package to extend the function module

When the package was introduced there were three things

  1. Open the file __init__.py aaa folder
  2. Since the package is a folder, you can not execute the package, so __init__.py file in the implementation of the package will be generated during the execution name stored in the package namespace (ie, the package name stored in the namespace comes from init.py)
  3. Aaa get a name in the current executable file, aaa is pointing package namespace

Three things happened import module

  1. Create a name space module
  2. Py file execution, the name of the implementation process generated stored in namespace
  3. Aaa get a name in the current executable file, aaa is pointing package namespace

Relative import, absolute imports

Absolute imports

# aaa/__init__.py

from aaa.m1 import func1
from aaa.m2 import func2

Relative imports

from .m1 import func1
from .m2 import func2



'''
包内模块是以相对路径导入

. 当前目录

.. 上一级目录

... 上上级目录

执行文件是以绝对路径导入。

'''

Precautions

  1. == module search path to the executable file as a standard. ==
  2. Package is a .py file
  3. All files in the package are imported using, instead of being run directly
  4. Introduced between the inner bag module can use absolute introduced (in the root directory as a reference packet) relative introduced (in the current directory where the module is introduced as a reference), recommended relative import
  5. When the file is executable file, not a relative import syntax, and only when the file is imported as a module within the file within the file in order to use the syntax of relative imports
  6. Those who a little while importing the left point must be a package import aaa.bbb.m3.f3error

Software development directory specification

Guess you like

Origin www.cnblogs.com/plf-Jack/p/10994782.html