Fix 'partially initialized module (circular import)' error? #1881
-
My Textual application has each widget split into separate files. I'm trying to add bindings to each widget to focus another specific widget using
I guessing this has something to do with the way widgets are mounted, but I'm not sure how best to fix the error or whether I'm just doing something completely wrong! Any advice would be much appreciated - here's a minimal reproduction of my code: # app.py
from textual.app import App, ComposeResult
from textual.widgets import Footer
from example_form import ExampleForm
from example_list import ExampleList
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield ExampleForm()
yield ExampleList()
yield Footer() # example_form.py
from textual.widgets import Input
from example_list import ExampleList
class ExampleForm(Input):
BINDINGS = [("escape", "cancel", "Cancel")]
def __init__(self) -> None:
super().__init__(placeholder="Add Item")
def action_cancel(self) -> None:
self.value = ""
self.app.query_one(ExampleList).focus() # example_list.py
from textual.widgets import ListView
from example_form import ExampleForm
class ExampleList(ListView):
BINDINGS = [("i", "focus_form", "Add Item")]
def action_focus_form(self) -> None:
self.app.query_one(ExampleForm).focus() |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
This is just a Python error really, nothing to do with Textual. If you take the above down to its minimum:
from widget_one import WidgetOne
from widget_two import WidgetTwo
from widget_two import WidgetTwo
class WidgetOne:
pass
from widget_one import WidgetOne
class WidgetTwo:
pass You end up with the same error:
As the error suggests, you have a circular import. |
Beta Was this translation helpful? Give feedback.
-
Two solutions come to mind. You could use a local import. def action_cancel(self) -> None:
from example_list import ExampleList
self.value = ""`
self.app.query_one(ExampleList).focus() Or you could specify the query as a string, which means you wouldn't strictly need to import anything. def action_cancel(self) -> None:
self.value = ""`
self.app.query_one("ExampleList").focus() |
Beta Was this translation helpful? Give feedback.
-
Thank you both for taking the time to answer - clearly the problem was the latter "I'm just doing something completely wrong!" then! Forgive my ignorance, I'm still learning Python and never had this error before. |
Beta Was this translation helpful? Give feedback.
Two solutions come to mind. You could use a local import.
Or you could specify the query as a string, which means you wouldn't strictly need to import anything.