Basic concepts
The package in Python, which is the folder that contains the __init__.py file.
For Python in-package import, that is, in-package modules import in-package modules, there are absolute import and relative import issues.
Search paths for ordinary Python modules
1. Search for the import module in the same path as the current module
2. Search the import module in the list of paths specified by the environment variable PYTHONPATH
3. Search the Import module in the list of paths specified by Sys.path
Steps for Python Import
Python All loaded module information is stored in the sys.modules dictionary structure, when the import of a module, the following steps are performed as follows
1. If import a, check if there is already a in sys.modules, if there is no load, if not, create a module object for a, and load a, you can repeat the import, but only one time.
2. If from a import B, first create a Module object for a, then resolve a, from which to look for B and fill in the __dict__ of a.
Relative import vs. absolute import
The absolute import format is import a.b or from A import B, and the relative import format is from. A import B or from: X import Y,. represents the current module ,: Represents the upper module,... represents the upper-level module, and so on.
Relative import maintenance benefits for packages
Relative import avoids the problem of package maintenance caused by hard coding, for example, if we change the name of a layer, then all the absolute imports of its child packages will not be used by other modules, but the module with relative import statements avoids this problem.
Note: modules that have relative import statements cannot be run directly. For example , for a digital.py file with the following hierarchy,
#!/usr/bin/env python#-*-coding:utf-8-*-###############################################################################purpose:to demo underlayer import upperlayer.################################################################################\phone#│common_util.py, Setup ()#│__init__.py#│#├─fax#│g3.py Bar ()#│__init__.py#│#├─mobile#│analog.py foo ()#│digital.py#│__init__.py#│#├─pager#│page.py#│__init__.py#│#└─voice#isdn.py#__init__.py############################################################################### from. AnalogImportFoo#valueerror:attempted relative import in Non-package from.. common_utilImportSetup#valueerror:attempted relative import in Non-package from.. Fax.g3ImportBar#valueerror:attempted relative import in Non-packageif __name__=='__main__': foo () setup () bar ()
If the above code runs directly, it will cause the valueerror exception,
valueerror:attempted relative import in Non-package
This is because a module runs directly, and Python believes that this module is the top-level module, there is no hierarchy, so no other relative paths can be found.
To run correctly, you must explicitly specify the path, as below,
c:\workspace\x_python>python-Phone.Mobile.Digital is from from are from Phone.Fax.G3
Of course, we don't normally run a module directly in the package, just a description.
The disadvantage of absolute import for package maintenance
For example, for a digital.py file with the following hierarchy,
#!/usr/bin/env python#-*-coding:utf-8-*-###############################################################################purpose:to demo underlayer import upperlayer.################################################################################\phone#│common_util.py, Setup ()#│__init__.py#│#├─fax#│g3.py Bar ()#│__init__.py#│#├─mobile#│analog.py foo ()#│digital.py#│__init__.py#│#├─pager#│page.py#│__init__.py#│#└─voice#isdn.py#__init__.py################################################################################From . Analog Import Foo # valueerror:attempted relative import in Non-package#From : Common_util Import Setup # valueerror:attempted relative import in Non-package#From .. FAX.G3 Import Bar # valueerror:attempted relative import in Non-package fromPhone.Mobile.AnalogImportFoo fromPhone.common_utilImportSetup fromPhone.Fax.G3ImportBarif __name__=='__main__': foo () setup () bar ()
The code above can be run directly.
However, absolute import of hard-coded mode, if there are many digital.py similar modules in the package, are used from the phone.common_util Import Setup statement, if one day to change the common_util the name of the package (folder) will affect all relevant code. There is no problem with a relative import.
However, absolute import is clearer, and if the package is not particularly complex and not particularly variable, it is recommended to use absolute import.
Finish.
Import issues within the Python package (absolute Import and relative import)