我有这个程序,我正在尝试使用 json 并查看有哪些人才可用,但是当我尝试运行人才时,我在导入时遇到错误

问题描述 投票:0回答:1

主要.py

from pygame import *
import pygame
from Talent_List import *
import requests
import json
import sys
import time


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 == "yes " or q == "y":
                continue

            else:
                print(" Goodbye, ending loop")
                self.active = False
                break

    def talent_reqs(self):
        pass


    def questions(self):
        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')

        attributes = [flame, gale, frost, thunder]

        return attributes

        def controller(self):
            pass


if __name__ == '__main__':
    Db = DeepwokenBuilder()
    Db.start()

人才.py

import json
from Main import DeepwokenBuilder


# make talent logic here ig

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):
        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)

                # if attributes < whatever

tt = Talents()
tt.display_talents()

错误:

ImportError: cannot import name 'DeepwokenBuilder' from partially initialized module 'Main' (most likely due to a circular import) (DeepwokenBuilder\Deepwoken Builder\Main.py)

我正在尝试找出为什么会出现错误。

python importerror circular-dependency
1个回答
1
投票

您在导入时遇到问题,因为您的逻辑存在真正的问题:

  1. 您创建了两个
    DeepwokenBuilder()
    实例,当您在一个实例中更改某些内容时,其他实例将不会有它。您应该只创建一个
    DeepwokenBuilder()
    并将其作为参数发送给其他类 - 作为
    Talents(db)
    tt.display_talents(db)
  2. 创建
    Talents()
    后,您必须在
    Main.py
    中创建
    DeepwokenBuilder
    db = DeepwokenBuilder()
    tt = Talents()
    tt.display_talents(db)
    db.start()

如果您更改它,则不必在

DeepwokenBuilder
中导入
Talents.py
,这可以解决
partially initialized module

的问题

完整的工作示例。

(PEP8:

lower_case_name.py
对于文件)

(PEP 8 -- Python 代码风格指南)

人才.py

import json
import os

class Talents:

    def __init__(self):

        #self.path = r'C:\Users\Zahir\Desktop\DeepwokenBuilder\Deepwoken Builder\Talent_list.json'
        self.base_dir = os.path.abspath(os.path.dirname(__file__))        
        self.path = os.path.join(self.base_dir, 'Talent_list.json')
        
        #print('[DEBUG] Talents: self.path:', self.path)
        
        with open(self.path) as f:
            self.data = json.load(f)
            
        self.flamecharm = self.data['flamecharm']
        self.galebreath = self.data['galebreath']
        self.frostdraw = self.data['frostdraw']
        self.thundercall = self.data['thundercall']
        self.base = self.data['base']

    def display_talents(self, db):

        attributes = db.questions()
        print(attributes)

        for attunement, value in self.data.items():
            for talent_name, talent_req in value.items():
                print(f"{talent_name, talent_req}, {attunement} ")
                print(attributes)

                # if attributes < whatever

main.py

#import pygame   # PEP8: `import *` is not preferred
from talents import Talents  # PEP8: `lower_case_names` for files with code - `talents.py` 

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? ")
            q = q.strip().lower()
            
            #if q !== "yes" and q != "y":
            if q not in ("yes", "y"):
                print(" Goodbye, ending loop")
                self.active = False
                break

    def questions(self):
        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')

        self.attributes = [flame, gale, frost, thunder]

        return self.attributes


if __name__ == '__main__':
    db = DeepwokenBuilder()   # PEP8: `lower_case_names` for variables
    tt = Talents()
    tt.display_talents(db)
    db.start()

示例 Talent_list.json 如果有人想测试它

{
"flamecharm": {"A": 1, "B": 2, "C": 3},
"galebreath": {"A": 1, "B": 2, "C": 3},
"frostdraw": {"A": 1, "B": 2, "C": 3},
"thundercall": {"A": 1, "B": 2, "C": 3},
"base": {"A": 1, "B": 2, "C": 3}
}
© www.soinside.com 2019 - 2024. All rights reserved.