A package is a way to organize the namespace of a Python module by using the module name.
Whether it is an import or a from...import form, it is necessary to realize--this is a package--when you encounter a dot in an imported statement (not in use).
A package is a directory-level folder that is used to form a py file (the nature of the package is a directory containing the __init__.py file).
Import file, the resulting namespace name from the file, import package, the name of the resulting namespace is also derived from the file, that is, the __init__.py under the package, the import package is the essence of the import of the file.
In the Python3, even if there is no __init__.py file under the package, import package will still not error, and in Python2, the package must have the file, or import packet error.
[[Email protected] module]#Tree. ├──bonf#Package│?? ├──__pycache__│??│??└──test1.cpython-36.pyc#The cache file generated when importing│??└──test1.py Module └──conf#Package├──__pycache__│??├──test2.cpython-36. Pyc│??└──test1.cpython-36. pyc├──test2.py#Module└──test.py#Module4 Directories, 6Files#content in the package#!/usr/bin/env pythondefFunc1 ():#There is a func1 function in the module test1 of the bonf package. Print("bonf") ~"bonf/test1.py"3L, 50C#another package conf#!/usr/bin/env pythondefFunc1 ():#test1 function func1 under CONF module under Package Print("function 1") ~"conf/test1.py"3L, 53C
Now to import these two packages
Import Bonf.test1 import conf.test1 # no error
Now to refer to the functions in them
>>> bonf.test1.func1 () bonf>>> conf.test1.func1 () # No problem function 1
As we can see, modules with the same name under two packages do not conflict, they come from their respective namespaces.
You can also use From-import, but this overrides the namespace.
from Import test1>>> test1.func1 () function 1fromimport test1>>> TEST1.FUNC1 () bonf
Multi-layered is also possible
Import bonf.donf.test10>>> donf.test10.func () # less than a layer, the path to Traceback (most Recent call last): '<stdin>' in <module> ' donf ' is not defined>>> bonf.donf.test10.func () 2 floor
Relative Import and Absolute import
from import test1 # Absolute fromimport test1 # relative
Python Basics-Package