Circular import error

hey quick question do you need to import code from another .py file into another to get rid of the undefined error if so how do i get rid of the circular import error

Please post your full error and link to your repl (not invite).

# In main.py
from other import main # Imports the function "main" from
# the file "other.py"
main()

main.py:

import tkinter as tk
window = tk.Tk()
window.title(“Hello world”)
window.geometry(“300x300”)

username = tk.Label(window,text=“username”)
password = tk.Label(window,text=“password”)
userentry=tk.Entry(window)
passentry=tk.Entry(window)
enter = tk.Button(window,text=“enter”,command=enterfuc())
create = tk.Button(window,text=“create new acount”,command=createfuc())
username.pack()
passentry.pack()
password.pack()
userentry.pack()
enter.pack()
create.pack()

tk.mainloop()

data.py:

from replit import db
keys = db.keys()
usernameinput=userentry.get()
passwordinput=passentry.get()
def createfuc():
if usernameinput in keys:
print(“username allready exsist”)
else:
db[“key”] = “value”
db[“key”] = “value”
def enterfuc():
if usernameinput==“show-keys” and passwordinput==“1234”:
print(keys)
if usernameinput and passwordinput in keys:
print(“test1”)
else:
print(“username or password is incorect”)

i am getting undifined errors on “createfuc()” and “enterfuc()” in main.py and “userentry.get()” and “passentry.get()” on data.py
link: https://replit.com/@MichaelFrankel/tkinter?v=1

Hello @MichaelFrankel! Please edit your post’s code to be formatted like this:

```python
Your code here
```

So that it is easier for staff and members of the community to help you with your issue!

Also see this guide on how to share your code:

1 Like

You need to import data in main.
Not sure this is the only error in your code but add

from data import *

Beginning of main.py and see

main.py:

import tkinter as tk
window = tk.Tk()
window.title("Hello world")
window.geometry("300x300")

username = tk.Label(window,text="username")
password = tk.Label(window,text="password")
userentry=tk.Entry(window)
passentry=tk.Entry(window)
enter = tk.Button(window,text="enter",command=enterfuc())
create = tk.Button(window,text="create new acount",command=createfuc())
username.pack()
passentry.pack()
password.pack()
userentry.pack()
enter.pack()
create.pack()

tk.mainloop()

data.py:

from replit import db
keys = db.keys()
usernameinput=userentry.get()
passwordinput=passentry.get()
def createfuc():
if usernameinput in keys:
print("username allready exsist")
else:
db["key"] = "value"
db["key"] = "value"
def enterfuc():
if usernameinput=="show-keys" and passwordinput=="1234":
print(keys)
if usernameinput and passwordinput in keys:
print("test1")
else:
print("username or password is incorect")

i am getting undifined errors on “createfuc()” and “enterfuc()” in main.py and userentry.get() and passentry.get() on data.py
link: https://replit.com/@MichaelFrankel/tkinter?v=1

Please edit your response, and use ``` instead of '''.
(Back ticks instead of apostrophes)

2 Likes

i tried this but it dident fix the errors in data and if i did both i got a circular import error

I looked at your repl.
You need to import data or main will not see what is in it.
But you do nit need to import main in data …

This also means that you should not reference anything declared in main in data, but the other way around yes

ok if you look at the project again it will give the error that “enterfuc” is not defined and when u hold the mouse curser over the “enterfuc” variable it will say it is defined though star imports

It says “might” be defined. As it does not know. You should also know that star imports are bad practice, and only use them when necessary.

You need to remove

from main import *

From data.py, as I said you cannot do that

1 Like

Yes but let’s first make sure he understand the fact that he cannot use variables created in main in data.py but inky viceversa as he is just going around in circles,

1 Like

that kinda sucks is there any way to get around circular import

Don’t import from main, create a third file or just put all the needed functions from main into the other file.

Your problem is that you seem not to understand the basic scoping rules.
So step back and put everything in main.py.

Take files as isolated programs. Data.py does not see what main has and viceversa and only on can import the other,

1 Like

yes i know that but i have ben trying to start organizing my cod lately because i made this cursed thing which i dont even know if it still works
https://replit.com/@MichaelFrankel/money-game-v3?v=1

Try to understand scoping.
Main is the core. Anything you take out of it cannot access what is in main except when passed as a function argument and after the file in imported in main.

x = 1
print(x)

If you split it it turns into

#data.py

def func(x):
  print(x)
#main.py
import data

x=1
data.func(x)

I always thought circular imports were when you try to import something from one file that imports from another file. For example:
data.py:

from main import *

main.py:

from data import *

this is because if you think about it, python cannot know if these two modules are dependent on each other (hence “circular”) lol I’m not good at explaining and you clearly know more about coding sorry to bud in

4 Likes