简体   繁体   English

导入在 Python 中导入另一个模块的模块

[英]Importing module that imports another module(s) in Python

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主窗口.py

import darkMotive
import tkinter as tk

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

darkMotive.py黑暗动机.py

import tkinter as tk
[other classes definitions]

My problem is that when I try to run app.py, I get this error:我的问题是,当我尝试运行 app.py 时,出现此错误:

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.但是当我单独运行 mainWindow.py 时,它可以工作。
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?模块darkMotive 不应该与mainWindow 一起导入app.py 的命名空间吗?

This didn't fit in the comments, but can you try the following?这不符合评论,但是您可以尝试以下操作吗?

mainWindow.py主窗口.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.您可能想要重命名mainWindow.py ,因为它与类名冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM