Introduction to how Python avoids circular import methods

Source: Internet
Author: User
In large python projects, there may be instances of cross-referencing between modules due to improper architecture design. The following article mainly to introduce you about how to avoid the problem of the circular import of Python data, the need for friends can refer to the following to see together.

Objective

Circular import issues are common when using the package in Python, and we create the following package to illustrate this issue:


pkg├──__init__.py├──module_a.py└──module_b.py

which

__init__.py designate pkg as a Python package

A function is defined in module_a.py that action_a() refers to a attribute in module_b.py, such as a function or variable

A function is defined in module_b.py that action_b() refers to a attribute in module_a.py, such as a function or variable

In this case, an error is thrown when the package is executed, that is, a circular import error circular reference, because module_a attempts to introduce Module_b, and Module_b first introduces Module_a, which causes the Python interpreter to be unable to proceed.

However, we can use some ingenious methods to make the above logic work properly while avoiding the errors introduced by the loop.

So, when does it work properly and when does it not work, and what is the reason for the normal work situation?

When does it work correctly?

1. Introduced at the top of module, do not use from, relative introduction, only valid in Python 2

At the top of the module import, for example import another_module , a function in module refers to a another_module.attribute function or variable in Another_module. This approach works because it is import another_module based on a relative reference to the current directory and is an implicit reference, which can be invalidated if the module is introduced from another package. In addition, import another_module this syntax is not supported in Python3, so do not use this method in your code to avoid the introduction of loops.

Such as:


# pkg/module_a.py from __future__ import print_functionimport module_b def action_a (): Print (module_b.action_b.__name__ )  # pkg/module_b.pyfrom __future__ import print_functionimport module_a def action_b (): Print (module_a.action_a.__ NAME__)

2. Introduce at the top of module, do not use from, absolutely introduce

At the top of the module, import, use the absolute path from the package, such as the import package.another_module function in module to refer to a package.another_module.attribute function or variable in Another_module. The reason for the introduction of the package name is that import .another_module this form of "relative ingestion" will report grammatical errors, while the absolute introduction of the package is suspended, and Python 2 and 3 support

Case:


# pkg/module_a.pyfrom __future__ Import print_functionimport pkg2.module_b def action_a (): Print (Pkg2.module_b.action_ b.__name__)  # pkg/module_b.pyfrom __future__ import print_functionimport pkg2.module_a def action_b (): Print ( PKG2.MODULE_A.ACTION_A.__NAME__)

3. The attribute of the another module is introduced at the bottom of module, not the another module, using the From

At the bottom of the module import (at least after the referenced attribute), the direct introduction another module of the attribute, such as from package.another_module import attribute , relative to the introduction is also supported, such as from .another_module import attribute , The function in module directly uses the referenced attribute.

Such as:


# pkg/module_a.pyfrom __future__ Import print_function def action_a (): Print (action_b.__name__) from. Module_b Import Act Ion_b  # pkg/module_b.pyfrom __future__ import print_function def action_b (): Print (action_a.__name__) from. Module_ A import action_a

4. The top of the function is introduced and can be used from

On the module's function top import, for example from package import another_module , also support the relative introduction, the introduction of module or attribute can be.

Such as:


# pkg/module_a.pyfrom __future__ Import print_function def action_a (): From. Import Module_b Print (module_b.action_b.__name__)  # pkg/module_b.pyfrom __future__ import print_function def Action_b (): From. Import module_a Print (module_a.action_a.__name__)

Or


# pkg/module_a.pyfrom __future__ Import print_function def action_a (): From. Module_b Import Action_b Print (Action_b.__na me__)  # pkg/module_b.pyfrom __future__ import print_functiondef action_b (): From. Module_a Import action_a Print ( ACTION_A.__NAME__)

While Python 2 and 3 support this approach, the coding is not elegant enough to affect code readability and is not recommended

Note

The issue discussed in this article is how to avoid loops when calling the package in Python

When executing a python module directly at the command line, the application is not exactly the same

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.