程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【Python】most likely due to a circular import

編輯:Python

Module import mechanism

When importing a module A, all imports in this module A and other modules in __init__.py where module A is located will be imported first, and the import of module A will be completed after importing these.

Generation of circular import

During the process of importing module A, other packages import the module itself

Workaround

  1. Introduce the package that causes the circular import into the function that uses it, that is, import it when the package is run
  2. If this module is only used for type hint, it is only used for static detection and not used at runtime.The following methods can be used.
from __future__ import annotations # must be placed at the beginning of the filefrom typing import TYPE_CHECKINGif TYPE_CHECKING:from module x import xxx
  1. Put the involved packages in a separate folder to avoid the joint import of other imports

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved