从python中的模块导入子类

从python中的模块导入子类,python,class,import,module,Python,Class,Import,Module,在导入一系列模块并访问其中的类时,我遇到了一个问题 这是我的代码: import sys, os for path, name, files in os.walk(os.getcwd()[:os.getcwd().rindex("Mario")+5]): sys.path.insert(0, os.path.join(path)) from pygame.locals import * import pygame, Screen WIDTH, HEIGHT = SIZE = 1200,

在导入一系列模块并访问其中的类时,我遇到了一个问题

这是我的代码:

import sys, os
for path, name, files in os.walk(os.getcwd()[:os.getcwd().rindex("Mario")+5]):
    sys.path.insert(0, os.path.join(path))
from pygame.locals import *
import pygame, Screen

WIDTH, HEIGHT = SIZE = 1200, 675
running = True
screen = pygame.display.set_mode(SIZE, SRCALPHA)
current = None
screen_menu = Screen.Menu().add_widget(Widget.Button(WIDTH/2-25, HEIGHT/2-25, 50, 30))
跑步时,我得到:

    Traceback (most recent call last):
  File "I:\Computing\Python\Mario\Global.py", line 5, in <module>
    import pygame, Screen
  File "I:\Computing\Python\Mario\screen\Screen.py", line 5, in <module>
    import pygame, Global
  File "I:\Computing\Python\Mario\Global.py", line 11, in <module>
    screen_menu = Screen.Menu().add_widget(Widget.Button(WIDTH/2-25, HEIGHT/2-25, 50, 30))
AttributeError: 'module' object has no attribute 'Menu'
[Finished in 2.9s with exit code 1]
回溯(最近一次呼叫最后一次):
文件“I:\Computing\Python\Mario\Global.py”,第5行,在
导入游戏、屏幕
文件“I:\Computing\Python\Mario\screen\screen.py”,第5行,在
导入pygame,全球
文件“I:\Computing\Python\Mario\Global.py”,第11行,在
screen_menu=screen.menu().添加_小部件(小部件按钮(宽度/2-25,高度/2-25,50,30))
AttributeError:“模块”对象没有“菜单”属性
[在2.9秒内完成,退出代码为1]

有人知道为什么会发生这种情况吗,我已经导入了它,菜单只是Screen中的一个类,如果需要,我可以给你Screen类

您有一个循环依赖--
Screen
使用
Global
Global
使用
Screen
。修改你的代码,不要有任何循环导入。

@Blender很遗憾,我试过了,它说,
ImportError:没有名为Menu的模块
在运行时修改
sys.path
通常是错误的。它使您的代码依赖于环境。对导入路径的任何更改都应该在环境中进行,并且有许多系统可以这样做。这会解决问题吗,或者这只是一个好的编程,而且我使用“循环”导入以一种易于阅读的方式来布局我的代码,因为我最初开始学习Java:)循环导入总是反映一种次优设计,可能会导致像你在这里看到的那样的错误。如果你看不出这个问题是由你的循环进口引起的(它是),那就更强烈地表明需要避免它们。这就成功了,谢谢你,我以后会记住的!:D