Quick introduction to Python import and Python Import
1. Absolute import and relative import
Absolute import: search by sys. path. First, the main directory (first entry ''in sys. path), and then the PYTHONPATH environment variable, standard library path, and pth specified path.
Relative import: search in the same package where the module is located. Note the differences between the package directory and the main directory.
Example 1: The following directory is available:
app/ __init__.py mod.py string.py
Mod. py content: import string
When Python mod. py is executed in the app/directory, it is absolutely imported. When python-m app. mod is executed in the app directory, it is relatively imported.
2. in python 2.7 and earlier versions, "relative" and "absolute" modules are first searched in sequence. That is to say, the module is first searched in the same package and then in sys. path.
In the preceding example, run python-m app in the app directory. when mod is used, the app/string will be imported. py (can be in string. print in py or in mod. add print string to py. _ file _ to test ).
Use the following statement to search for only absolute paths:
From _ future _ import absolute_import
Add this statement at the beginning of mod. py. When you execute python-m app. mod in the app upper-level directory, the string module in the standard library will be imported.
In python3.3, only absolute paths are searched by default. To use relative import, run the following statement:
From. import string
Note: The start vertex can only be used in the from statement, but not in the import Statement.
3. Use the _ name _ attribute of the module to determine the position of the module in the package structure. When the _ name _ attribute does not contain package information (I. e. useless '. 'indicates the hierarchy, for example,' _ main _ '), the module is parsed as the top-level module, regardless of the actual position of the module in the file system.
Example 2:
app/ __init__.py sub1/ __init__.py mod1.py sub2/ __init__.py mod2.py
Try to import mod2.py in mod1.py and add from .. sub2 import mod2.
If you run python mod1.py directly in the sub1 directory or python sub1/mod1.py in the app directory, the following error occurs: "Attempted relative import in non-package ".
If you run python-m sub1.mod1 in the app directory, the following error occurs: "Attempted relative import beyond toplevel package ".
The correct method is to execute python-m app in the app directory. sub1.mod1, or do not use from .. sub2 import mod2 and use another method (for example, add sub2 to sys. path ).
Example 3:
__init__.pystart.pyparent.pysub/ __init__.py relative.py
Start. py contains import sub. relative, and relative. py contains from .. import parent.
If you run python start. py, the following error occurs: "Attempted relative import beyond toplevel package ".
Solution: Create a pkg directory, move the parent. py and sub directories to the pkg directory, and change start. py to import pkg. sub. relative.
Summary
The preceding section describes how to quickly import Python data. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!