Introduction to the

We move on to discuss the mechanisms of import, focusing on the concepts of absolute versus relative imports.

Import Running process

How does import work when we use Import OS?

Python first searches sys.modules for a module named OS. If sys.modules is present in the cache, the contents of the cache map are returned directly, and the import process ends.

If there are no OS modules in the cache, Python continues to search its list of built-in modules. These modules are pre-installed by Python, which is simply the Python standard library, and OS is the standard library, so the import process ends there.

If it is still not found in the built-in module, the module is searched in the path defined in the sys.path list.

When Python finds a module, it initializes the corresponding Module object in the local scope so that we don’t get an error when using the module in the current file.

Python’s import mechanism is very flexible, which also introduces some serious security risks, such as using the import mechanism to rewrite Python’s core functions or using the import mechanism to execute malicious code.

Malicious developers will submit their own malicious third-party packages to PyPi for download. These packages often have similar names and functionality to popular packages, but with more malicious code, such as GET and POST requests from Requests. Malicious developers can download the source code for Requests, add malicious code, and submit it to PyPi as request. Malicious code can use the import mechanism to rewrite Python and gain a degree of control over your device.

To avoid this, you should only confirm the name of the third-party library when installing it.

Security and flexibility are like having your cake and eating it too.

The standard way to write import statements

When writing import statements, whether you use relative or absolute imports, follow the advice mentioned in PEP 8 to make your code look more elegant

1. The import statement should unload the top of the file, but after the module (.py file) comment or caption

2. Import statements are classified according to the imported content. Generally, they are classified into three categories. Class 1: Import Python built-in modules. Type 2: Import related third-party library modules Type 3: Import local modules of the application (that is, modules of the current application)

3. When importing modules of different categories, separate them with blank lines

The criteria are as follows:

""" This is the import module standard recommended by PEP 8

# Built-in modules
import os
import time

# Third-party modules
import flask

# local module
from test import test1
Copy the code

Absolutely Import Absolute Import

Both absolute and relative import need a reference, otherwise the concept of “absolute” and “relative” would be impossible to talk about.

The absolute import reference is the root folder of the project.

Absolute imports require us to use the full path from the root folder of the project to the module to be imported.

Suppose we have the following directory structure:

└ ─ ─ project ├ ─ ─ package1 │ ├ ─ ─ module1. Py │ └ ─ ─ module2. Py └ ─ ─ package2 ├ ─ ─ just set py ├ ─ ─ module3, py ├ ─ ─ module4, py └ ─ ─ subpackage1 └ ─ ─ module5. PyCopy the code

The root folder is project, which contains package1 and package2 folders. Under Package1 there are module1.py and module2.py modules. Py, module3.py and module4.py, and subPackage1, which contains the module5.py module.

Package1 /module2.py contains a function called function1. Package2 /__init__.py contains a class named calss1. Package2 subpackage1 / module5. Py contains a function called function2.

Here is an example of an absolute path import:

from package1 import mudule1
from package1.module2 import function1
from package2 import class1
from package2.subpackage1.module5 import function2
Copy the code

“Absolute paths require that we provide a full and detailed import path for each package or module, starting with the topmost folder.”

Absolute imports are the default import form in Python3.x, and the recommended import form in PEP 8, which is straightforward enough to know exactly where the package or module to import is.

In addition, “the path position of the module (.py file) that uses absolute imports has changed, so absolute imports still take effect”, but if the path of the object that uses absolute imports has changed, then you need to rewrite the absolute import statement.

But there are times when absolute imports are verbose and complex, and that’s when you can try relative imports

Relative to the import

The reference of relative import is the current position. When we use relative import, we need to give the relative and current position and the location of the resource to be imported.

In detail, there are two kinds of relative import, respectively “implicit relative import” and “display relative import”, simply mention a mouth.

The directory structure is still as follows

└ ─ ─ project ├ ─ ─ package1 │ ├ ─ ─ module1. Py │ └ ─ ─ module2. Py └ ─ ─ package2 ├ ─ ─ just set py ├ ─ ─ module3, py ├ ─ ─ module4, py └ ─ ─ subpackage1 └ ─ ─ module5. PyCopy the code

Package1 /module2.py contains a function called function1. Package2 /__init__.py contains a class named calss1. Package2 subpackage1 / module5. Py contains a function called function2.

Then reference module4.py in package2/module3.py, which is written as follows

# package2/module3.py
import module4 Implicit relative import
from . import module4 Explicit relative import
from package2 import module4 # absolute import
Copy the code

Implicit relative imports, which do not explicitly tell Python to import relative to the current location, are no longer recommended in Python3.x, mainly because implicit relative imports are unclear.

If you are still using python2.x, you can disable implicit relative imports by using from __future__ import absolute_import.

If you want to use function1 in package1/module1.py, use relative imports to write this

# package1/module1.py
from .module2 import function1 Module2 is used because module2 and module1 are in the same directory
Copy the code

If you want to import class1 and function2 in package2/module3.py, use relative imports to write this

# package2/module3.py
from . import class1 # single point, meaning import class1 from the current package, specifically from __init__.py
from .subpackage1.module5 import function2 Since subPackage1 is in the same directory as module3.py, use.subpackage1
Copy the code

Relative imports are generally cleaner than absolute imports, but for some projects where directory structures may change, relative imports are less intuitive and can be confusing.

The tail

This article is a quick introduction to relative and absolute import concepts.

If this article is helpful to you, please click “look” to support, this is very important to this trumpet.