Absolute import is actually very simple, that is, to use the from statement to specify the top-level package name before import. the following two examples illustrate the absolute import statement from .. import in Python.
Relative or absolute import
More complex parts have been implemented since python2.5: to import a module, you can specify absolute or package-relative imports. This plan will be moved to the details that make the absolute import the default in other versions of python.
Assume that you have a package directory, as shown below:
pkg/pkg/__init__.pypkg/main.pypkg/string.py
A package named pkg contains two sub-modules, pkg. main and pkg. string. Consider the code in 'Main. py'. what will happen if we execute the statement import string in Python or earlier? It will query the Directory of the package and execute a relative import to find pkg/string. py, the content of the imported file is pkg. string Module. the boundary name of this module is 'string' in pkg. the namespace in the main module.
If pkg. string is what you want, this is very good. But if you only want the basic string Module of python?
There is no clear way to ignore pkg. string and find the basic module. In general, you have to check the content in sys. modules, which is slightly unclear. Py of Holger Krekel. the std package provides a neat method for importing methods from the basic library, improt py; py. std. string. jion (), but the package installation process in python is not available.
Reading the code is not clear enough in terms of relative import, because the reader may confuse the use of the string and pkg. string modules. Python users can immediately know that the names are different between the basic library and their own package modules, but you cannot protect your own sub-module names in a new version of python.
From python2.5, you can open the import action and use a from _ future _ import absolute_import. This absolute import behavior will become a default detail in future python. Once the absolute import is defaulted, the import string will always look for the basic library. We recommend that you use absolute import as much as possible, so it is appropriate to use from pkg improt string in your code. In python2. *, you must:
from __future__ import absolute_import
Example 1:
For example, the code in module A. B .C:
From. import D # import. b. dfrom .. import E # import. efrom .. F import G # import A.F. G ,.. it is connected to F and there is no space in the middle
. Indicates the current directory,... indicates the previous directory, and... indicates the upper directory.
Example 2:
Directory structure:
package/ __init__.py subpackage1/ __init__.py moduleX.py moduleY.py subpackage2/ __init__.py moduleZ.py moduleA.py
You can import the module in subpackage1/moduleX. py or subpackage1/_ init _. py as follows:
from .moduleY import spamfrom .moduleY import spam as hamfrom . import moduleYfrom ..subpackage1 import moduleYfrom ..subpackage2.moduleZ import eggsfrom ..moduleA import foofrom ...package import barfrom ...sys import path
Note: from... sys import path is legal, but it is not recommended. Directly from sys import path.