Python循环导入错误排查:无法从Main导入DeepwokenBuilder
循环导入错误排查与修复
错误原因
你遇到的ImportError是循环导入导致的:
- 运行
Talents.py时,它会先执行from Main import DeepwokenBuilder,触发Main.py的加载流程。 - 但
Main.py是自上而下加载的,在它还没完成DeepwokenBuilder类的定义时,Talents.py就试图导入这个类,Python因此报错提示模块未完全初始化。
另外你的代码逻辑存在冗余依赖:Talents.display_talents()只需要获取用户输入的属性值,完全没必要依赖整个DeepwokenBuilder类,这也是引发不必要导入的核心原因。
修复方案
方案1:拆分公共功能到独立模块(推荐)
把获取属性输入的逻辑抽成独立模块,彻底消除模块间的循环依赖:
- 创建
utils.py文件,写入公共方法:
def get_attributes(): flame = input("How much flamecharm do you have?\n") gale = input("How much galebreath do you have?\n") frost = input("How much frostdraw do you have?\n") thunder = input('How much thunder do you have?\n') return [flame, gale, frost, thunder]
- 修改
Main.py,替换原有的questions()方法:
from pygame import * import pygame from Talent_List import * import requests import json import sys import time from utils import get_attributes # 导入公共方法 class DeepwokenBuilder: def __init__(self): self.active = True def start(self): while self.active: print(" Starting... welcome to the deepwoken talent checker! \n") self.questions() q = input("Would you like to continue? ") # 修正空格问题,同时兼容大小写 if q.strip().lower() in ["yes", "y"]: continue else: print(" Goodbye, ending loop") self.active = False break def talent_reqs(self): pass def questions(self): return get_attributes() # 调用公共方法 def controller(self): pass # 修正原代码的缩进错误 if __name__ == '__main__': Db = DeepwokenBuilder() Db.start()
- 修改
Talents.py,直接导入公共方法替代DeepwokenBuilder:
import json from utils import get_attributes # 导入公共方法 with open(r'C:\Users\Zahir\Desktop\DeepwokenBuilder\Deepwoken Builder\Talent_list.json') as f: Talent_list = json.load(f) class Talents: def __init__(self): self.Talent_list = Talent_list self.flamecharm = Talent_list['flamecharm'] self.galebreath = Talent_list['galebreath'] self.frostdraw = Talent_list['frostdraw'] self.thundercall = Talent_list['thundercall'] self.base = Talent_list['base'] def display_talents(self): attributes = get_attributes() # 直接调用公共方法获取输入 print(attributes) # 优化循环变量命名,提升可读性 for attunement, talents in self.Talent_list.items(): for talent_name, talent_req in talents.items(): print(f"{talent_name}, {talent_req}, {attunement} ") print(attributes) # 在这里编写天赋匹配逻辑 tt = Talents() tt.display_talents()
方案2:延迟导入(临时修复,不推荐)
如果不想拆分模块,可以把导入操作延迟到方法内部,避开模块加载时的冲突:
import json with open(r'C:\Users\Zahir\Desktop\DeepwokenBuilder\Deepwoken Builder\Talent_list.json') as f: Talent_list = json.load(f) class Talents: def __init__(self): self.Talent_list = Talent_list self.flamecharm = Talent_list['flamecharm'] self.galebreath = Talent_list['galebreath'] self.frostdraw = Talent_list['frostdraw'] self.thundercall = Talent_list['thundercall'] self.base = Talent_list['base'] def display_talents(self): from Main import DeepwokenBuilder # 延迟导入到方法内部 db = DeepwokenBuilder() attributes = db.questions() print(attributes) for i, v in self.Talent_list.items(): attunement = i for i, v in v.items(): talent_name = i talent_req = v print(f"{talent_name, talent_req}, {attunement} ") print(attributes) tt = Talents() tt.display_talents()
这种方法虽然能快速解决错误,但会让代码依赖关系模糊,不利于长期维护。
额外注意点
- 原
Main.py中controller方法缩进错误,被嵌套在questions方法内部,已在方案1中修正。 - 原
Main.py的判断条件q == "yes "多了空格,会导致输入"yes"时不匹配,建议使用strip().lower()做兼容处理。
内容的提问来源于stack exchange,提问作者Devair
相关产品推荐
相关产品推荐

