Use of a module package

Use package

1. The first thing to import module of three things happen

先产生一个执行文件的名称空间:
1.创建模块文件的名称空间
2.执行模块文件中的代码 将产生的名字放入模块的名称空间中
3.在执行文件中拿到一个指向模块名称空间的名字

2. What is the package?

It is a combination of a series of modules file, expressed in the form of a folder is
the folder inside there is usually a __init__.py file
is essentially a package or module

3. The first thing happened import package

First import the package:
first generate a namespace executable file
1. Create a namespace package following __init__.py file
2. Execute the following code package __init__.py file will have the name of your bag below _ _init__.py file name space
3. get the name of a pointer to the following package __init__.py file name space in the executable file

4. 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, the module designers management module, maintenance will be more complicated, so we can use the package to extend the function of the module.

5.python2 and Python3 import Pack Notes

python2 If you are importing must have a __init__.py file bag below
python3 the bag if you want to import the following file __init__.py no error will not have
the time when you delete unnecessary program files do not arbitrarily delete __init__ .py file

== bag module, used to package the introduction

Package file folder containing __init__.py; leader packet is introduced __init__

'''
包一定是被当作模块文件导入,模块文件 m1.py/m2.py 的搜索路径以执行文件 包的介绍.py 路径为准,在执行文件所在的文件夹只能看到包aaa,而找不到包aaa内的m1.py文件。
'''
#包   
##aaa
###__init__.py
from m1 import f1
###m1.py
def f1():
    print('来自m1中的f1')
##包的介绍.py(执行文件)
import aaa
print(aaa.f1)
'''
打印结果
ModuleNotFoundError: No module named 'm1'
'''
#正确的导包的方式,绝对导入(一层包)
#包
##包的介绍.py(执行文件)
import aaa   #包aaa
print(aaa.f1)
print(aaa.f2)
###__init__.py # 包aaa内文件
from aaa.m1 import f1 # 不符合包的原则
from aaa.m1 import f2  # 不符合包的原则
###m1.py  # 包aaa内文件
def f1():
    print('来自m1中的f1')

def f2():
    print('来自m1中的f2')
'''
<function f1 at 0x000000000BC94F28>
<function f2 at 0x000000000BCA7048>
'''
#正确的导包的方式,绝对导入(二层包)
#包
##包的介绍.py(执行文件)
import aaa #包aaa
print(aaa.f5)
print(aaa.f5())
###__init__.py  # 包aaa内文件
from aaa.bbb.m3 import f5 # 不符合包的原则
###m1.py  # 包aaa内文件
def f1():
    print('来自m1中的f1')
def f2():
    print('来自m1中的f2')
###包bbb
####__init__.py #包bbb内的文件
    pass
####m3.py # #包bbb内的文件
def f5():
    print('来自m3中的f5')
def f6():
    print('来自m3中的f6')
'''
<function f5 at 0x0000000009FF3F28>
来自m3中的f5
'''
或者直接可以在执行文件内还是以绝对导入的方式导入f5也是一样的。
from aaa.bbb.m3 import f5
print(f5)
print(f5())

The relative and absolute Import introduced

Absolute Import: You must perform file search path as a benchmark, such as the above from aaa.bbb.m3 import f5

Relative imports:

. Represent files in the current folder

. . Indicates that the file folder under his father

. . . Indicates that the file grandfather folder

#正确的导包的方式,相对导入(二层包)
#包
##包的介绍.py(执行文件)
import aaa #包aaa
print(aaa.f5)
print(aaa.f5())
###__init__.py  # 包aaa内文件
from aaa.bbb import f5
###m1.py  # 包aaa内文件
def f1():
    print('来自m1中的f1')
def f2():
    print('来自m1中的f2')
###包bbb
####__init__.py #包bbb内的文件
    from .m3 import f5
####m3.py # #包bbb内的文件
def f5():
    print('来自m3中的f5')
def f6():
    print('来自m3中的f6')
'''
<function f5 at 0x000000000A008048>
来自m3中的f5
'''

6. The lead pack notes

  1. All files in the package are imported using, instead of being run directly
  2. 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
  3. When the file is executable file, you can not use the syntax relative imports, only the file is imported as a module within the file within the file in order to use the syntax of relative imports
  4. Those who a little while importing the left point must be a package import aaa.bbb.m3.f5error

Guess you like

Origin www.cnblogs.com/zhangchaocoming/p/11605613.html