0

I don't understand these errors. by the way I have removed the file names. I just separated my code into a couple of different files for easier access. imported them into the main one and at first, it didn't work when I directly imported them but when I imported them as a different name it worked. Also below is my main.py code if it helps. This code just repeats itself over and over.

from time import sleep as s
from sys import exit

def opening():
    q = 0
    print("Welcome to hangman")
    s(1)
    print("Let's get started by welcoming the players.")
    s(1)
    while q == 0:
        contd = input("Do you want to carry on or quit? ").lower()
        contdornot = ["carry on", "quit"]
        if contd in contdornot:
            if contd == contdornot[0]:
                break
            elif contd == contdornot[1]:
                print("Goodbye")
                exit()
        elif contd not in contdornot:
            print("Please say 'carry on' or 'quit'")
            s(1)

opening()

def players():
    i = 0
    player1 = input("Player 1, what is your name: ")
    print(f"Welcome {player1}!")
    s(1)
    while i == 0:
        player2 = input("Player 2, what is your name: ")
        if player2.lower() != player1.lower():
            print(f"Welcome {player2}!")
            break
        elif player2.lower() == player1.lower():
            print("Name cannot be the same as player 1!")
    s(1)

players()

import wordchoosing as wc
import guessing as gs
import decidingturn as dt

person, oppositeplayer = dt()

wordname = wc()

tries = gs.letter_guess1()
  • 2
    It's hard to know without seeing your code, but this is likely caused by a circular import, just like the error states. A circular import occurs when you have a module A that imports module B, but module B imports module A. Under some circumstances, this can introduce some ambiguity when Python tries to run your code. – tedtanner Jan 29 '22 at 18:05
  • 1
    It seems you likely have a circular import. main.py imports wordchoosing and wordchoosing.py imports main. Same for main.py and guessing.py. – jodag Jan 29 '22 at 18:07
  • Alternatively, you might have named a file the same thing as one of your imports. If you have `requests.py` and inside you `import requests` (from Python's std lib), Python might think you have a circular dependency. – tedtanner Jan 29 '22 at 18:09
  • @tedtanner if you have visual studio code, I think i can send you my folder and you can examine my code if you want. – user18051841 Jan 29 '22 at 18:17
  • @jodag And how may i fix this? – user18051841 Jan 29 '22 at 18:18
  • The fix would depend on your code and why you have these circular imports in the first place. I recommend organizing your modules in a heirarchy. If main is your entrypoint then nothing should import from main. Any functions you need from main in another file should be moved elsewhere, possibly to a new file. – jodag Jan 29 '22 at 18:29

1 Answers1

2

Here's what happened:

You are running main.py file, which has this line import wordchoosing as wc on line 4. In wordchoosing.py module you have this line from main import person, oppositeplayer.

So Python goes from main.py to wordchoosing.py and then back to main.py. Up until now your main.py module is not fully executed (just the lines above the import wordchoosing statement are executed - line 1 to 4).

Python is now in line 5 in main.py module which you have import guessing as gs. While executing guessing.py, it sees from main import wordname, oppositeplayer in line 1.

When Python looks back at main.py module, only the lines above line 5 is executed not the whole module. So if there is a symbol name 'wordname' in the rest of the code, it is not in it's global namespace yet.

This is called circular import. Check here to see how you can avoid it.

S.B
  • 13,077
  • 10
  • 22
  • 49
  • So are you suggesting that i should import it after wordname is defined? – user18051841 Jan 29 '22 at 18:14
  • @user18051841 check the edited answer – S.B Jan 29 '22 at 18:25
  • Thank you for explaining that to me. I moved the imports to where the variables and already been defined and right before the functions were about to be called and I am pretty sure that it is fixed. – user18051841 Jan 29 '22 at 18:33
  • @user18051841 happy to help, yes that is a possible fix to it. Another fix is to defer import until later(in the body of the function for example). But circular imports usually imply that you need to structure your package in a different way. `main.py` should be the place where the other modules are imported in. The other modules should not import anything from the entry point. – S.B Jan 29 '22 at 18:35
  • I have edited the question to include my code from maiin.py if it is any use since it still does not work. I don't get any more errors but when it gets to the part where the functions in the other files are supposed to be called, it just repeats main.py. It starts over. – user18051841 Jan 29 '22 at 18:48