Importing module that imports another module(s) in Python

Viewed 76

I'm currently trying to rewrite an old app, and becuase first things first, I want to clean up the structure a little bit. I encountered a problem with importing modules, and I've exhausted few options found over the web, so it's time to ask for help.

The relevant structure is:

root/
   __init__.py
   app.py
   interface/
      __init__.py
      darkMotive.py
      mainWindow.py

app.py

import interface.mainWindow

if __name__ == "__main__":
    root = tk.Tk()
    main = mainWindow(root)
    title = root.title("app")
    main.pack(side="top", fill="both", expand=True)
    root.mainloop()

mainWindow.py

import darkMotive
import tkinter as tk

class mainWindow(tk.Frame):
    def __init__(self, *args, **kwargs):
    [rest of the code]

darkMotive.py

import tkinter as tk
[other classes definitions]

My problem is that when I try to run app.py, I get this error:

Traceback (most recent call last):
  File "X:/x/xxx/app.py", line 2, in <module>
    import interface.mainWindow
  File "X:\x\xxx\interface\mainWindow.py", line 1, in <module>
    import darkMotive
ModuleNotFoundError: No module named 'darkMotive'

But when I run mainWindow.py alone, it works.
What do I do wrong? Could you point me in the right direction? Shouldn't module darkMotive be imported with mainWindow into app.py's namespace?

1 Answers

This didn't fit in the comments, but can you try the following?

mainWindow.py

from interface import darkMotive

app.py

from interface.mainWindow import mainWindow

You probably want to rename mainWindow.py, because it clashes with the class name.

Related