python – Cant import other classes from circular import

I am trying to make a game about being the president, and I have cards for actions you can do. I need to make the cards do stuff, so I am refreshing the screen and changing global variables, but it throws this:

    Traceback (most recent call last):
        File "C:UsersFobPycharmProjectsPresidentmain.py", line 1, in <module>
    import cards
        File "C:UsersFobPycharmProjectsPresidentcards.py", line 7, in <module>
    import main
        File "C:UsersFobPycharmProjectsPresidentmain.py", line 34, in <module>
    refresh()
         File "C:UsersFobPycharmProjectsPresidentmain.py", line 30, in refresh
    cards.create_cards()
    AttributeError: partially initialized module 'cards' has no attribute 'create_cards' (most 
    likely due to a circular import)

When I remove the main import from cards, it will work. I have tried debuggers, but the ones I tried have not worked.
Here is the code to the main class:

from globals import *
import menus
import cards

def back_cmd():
    clear()
    refresh()

def clear():
    for ele in globals.root.winfo_children():
        print(ele)
        ele.destroy()

def version_log():
    clear()
    version_list = Label(text = f"Current version: {globals.version}n"
                                      f"Added card playing system, and also squaresn"
                                      f"at the bottom of each card showing how it effectsn"
                                      f"your stats. Added statistics.", font = ("Arial", 20))
    back_button = Button(text = "Back", width = 25, height= 5, bg = 'Gray', command=back_cmd)

    version_list.pack()
    back_button.pack()

def refresh():
    root.geometry("640x640")
    root.title('Cool')
    menus.get_home()
    cards.create_cards()
    version_button = Button(text="Version 0.01", command=version_log)
    version_button.pack()

refresh()
root.mainloop()

Here is the code to the cards class:

import globals
import random
from tkinter import *
from PIL import Image, ImageTk

import menus
import main




def generate_card():
    rand_int = random.randint(0, 10)
    if(rand_int == 1):
        return ["Declare tacos as a sandwich", "121"]
    elif(rand_int == 2):
        return ["Rename french friesn to freedom fries", "211"]
    elif(rand_int == 3):
        return ["Make all shoes velcro", "312"]
    elif(rand_int == 4):
        return ["Everyone has to nwear sunglasses", "321"]
    elif(rand_int == 5):
        return ["Ban pepper throughout nthe country", "132"]
    elif(rand_int == 6):
        return ["Stop barbie doll production", "312"]
    elif(rand_int == 7):
        return ["Give everyone free paper", "312"]
    elif(rand_int == 8):
        return ["Give a speech", "222"]
    else:
        return ["Make all backpacks blue", "121"]


def deal_effect(s):
    s = str(s)
    count = 0
    for x in s:
        if(x == "1"):
            if(count == 0):
                globals.rep -= (2 + random.randint(1, 3))
                count += 1
                
            elif(count == 1):
                globals.fed_budg -= (20000000000 + random.randint(1000000000, 7000000000))
                count += 1
                
            elif(count == 2):
                globals.global_opinion -= (2 + random.randint(1, 5))
                count += 1
                
        if (x == "2"):
            if (count == 0):
                globals.rep -= (1 + random.randint(1, 3))
                count += 1
                
            elif (count == 1):
                globals.fed_budg -= (4000000000 + random.randint(5000000, 1500000000))
                count += 1
                
            elif (count == 2):
                globals.global_opinion -= (1 + random.randint(0,1))
                count += 1
                
        if (x == "3"):
            if (count == 0):
                globals.rep += (1 + random.randint(1, 3))
                count += 1
                
            elif (count == 1):
                globals.fed_budg += (4000000000 + random.randint(50000000, 1500000000))
                count += 1
                
            elif (count == 2):
                globals.global_opinion += (1 + random.randint(0, 2))
                count += 1

        main.refresh()

                

def get_color(arg):
    arg = int(arg)
    if(arg == 0):
        return 'red'
    elif(arg == 1):
        return 'red'
    elif(arg == 2):
        return 'orange'
    elif(arg == 3):

        return 'green'




def create_cards():
    card_one_status = generate_card()
    card_two_status = generate_card()

    # print(card_one_info[0])



    print("reached")
    card_one = Button(globals.root, text = card_one_status[0], fg = "Black", bg = "White"
                      , height = 20, width=25, command=deal_effect(card_one_status[1]))
    card_two = Button(globals.root, text = card_two_status[0], fg = "Black", bg = "White"
                      , height = 20, width=25, command=deal_effect(card_two_status[1]))

    if(globals.forecast == True):
        one = Canvas(globals.root, bg='white', height=20, width=25)
        two = Canvas(globals.root, bg='white', height=20, width=25)
        three = Canvas(globals.root, bg='white', height=20, width=25)

        one.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][0]),
                             outline=get_color(card_one_status[1][0]))
        two.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][1]),
                             outline=get_color(card_one_status[1][1]))
        three.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][2]),
                               outline=get_color(card_one_status[1][2]))

        one.place(x=70, y=350)
        two.place(x=140, y=350)
        three.place(x=210, y=350)

        one = Canvas(globals.root, bg='white', height=20, width=25)
        two = Canvas(globals.root, bg='white', height=20, width=25)
        three = Canvas(globals.root, bg='white', height=20, width=25)

        one.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][0]),
                             outline=get_color(card_two_status[1][0]))
        two.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][1]),
                             outline=get_color(card_two_status[1][1]))
        three.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][2]),
                               outline=get_color(card_two_status[1][2]))


    print(card_one_status[1][2])
    one.place(x=407, y=350)
    two.place(x=479, y=350)
    three.place(x=549, y=350)


    card_one.place(x = 60, y = 70)
    card_two.place(x=400, y = 70)
import random
from tkinter import *
from PIL import Image, ImageTk

import menus
import main




def generate_card():
    rand_int = random.randint(0, 10)
    if(rand_int == 1):
        return ["Declare tacos as a sandwich", "121"]
    elif(rand_int == 2):
        return ["Rename french friesn to freedom fries", "211"]
    elif(rand_int == 3):
        return ["Make all shoes velcro", "312"]
    elif(rand_int == 4):
        return ["Everyone has to nwear sunglasses", "321"]
    elif(rand_int == 5):
        return ["Ban pepper throughout nthe country", "132"]
    elif(rand_int == 6):
        return ["Stop barbie doll production", "312"]
    elif(rand_int == 7):
        return ["Give everyone free paper", "312"]
    elif(rand_int == 8):
        return ["Give a speech", "222"]
    else:
        return ["Make all backpacks blue", "121"]


def deal_effect(s):
    s = str(s)
    count = 0
    for x in s:
        if(x == "1"):
            if(count == 0):
                globals.rep -= (2 + random.randint(1, 3))
                count += 1
                
            elif(count == 1):
                globals.fed_budg -= (20000000000 + random.randint(1000000000, 7000000000))
                count += 1
                
            elif(count == 2):
                globals.global_opinion -= (2 + random.randint(1, 5))
                count += 1
                
        if (x == "2"):
            if (count == 0):
                globals.rep -= (1 + random.randint(1, 3))
                count += 1
                
            elif (count == 1):
                globals.fed_budg -= (4000000000 + random.randint(5000000, 1500000000))
                count += 1
                
            elif (count == 2):
                globals.global_opinion -= (1 + random.randint(0,1))
                count += 1
                
        if (x == "3"):
            if (count == 0):
                globals.rep += (1 + random.randint(1, 3))
                count += 1
                
            elif (count == 1):
                globals.fed_budg += (4000000000 + random.randint(50000000, 1500000000))
                count += 1
                
            elif (count == 2):
                globals.global_opinion += (1 + random.randint(0, 2))
                count += 1

        main.refresh()

                

def get_color(arg):
    arg = int(arg)
    if(arg == 0):
        return 'red'
    elif(arg == 1):
        return 'red'
    elif(arg == 2):
        return 'orange'
    elif(arg == 3):

        return 'green'




def create_cards():
    card_one_status = generate_card()
    card_two_status = generate_card()

    # print(card_one_info[0])



    print("reached")
    card_one = Button(globals.root, text = card_one_status[0], fg = "Black", bg = "White"
                      , height = 20, width=25, command=deal_effect(card_one_status[1]))
    card_two = Button(globals.root, text = card_two_status[0], fg = "Black", bg = "White"
                      , height = 20, width=25, command=deal_effect(card_two_status[1]))

    if(globals.forecast == True):
        one = Canvas(globals.root, bg='white', height=20, width=25)
        two = Canvas(globals.root, bg='white', height=20, width=25)
        three = Canvas(globals.root, bg='white', height=20, width=25)

        one.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][0]),
                             outline=get_color(card_one_status[1][0]))
        two.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][1]),
                             outline=get_color(card_one_status[1][1]))
        three.create_rectangle(0, 0, 100, 100, fill=get_color(card_one_status[1][2]),
                               outline=get_color(card_one_status[1][2]))

        one.place(x=70, y=350)
        two.place(x=140, y=350)
        three.place(x=210, y=350)

        one = Canvas(globals.root, bg='white', height=20, width=25)
        two = Canvas(globals.root, bg='white', height=20, width=25)
        three = Canvas(globals.root, bg='white', height=20, width=25)

        one.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][0]),
                             outline=get_color(card_two_status[1][0]))
        two.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][1]),
                             outline=get_color(card_two_status[1][1]))
        three.create_rectangle(0, 0, 100, 100, fill=get_color(card_two_status[1][2]),
                               outline=get_color(card_two_status[1][2]))


    print(card_one_status[1][2])
    one.place(x=407, y=350)
    two.place(x=479, y=350)
    three.place(x=549, y=350)


    card_one.place(x = 60, y = 70)
    card_two.place(x=400, y = 70)


does anyone know why? Sorry about how long it is Thanks!

Leave a Comment