【发布时间】:2013-05-15 15:38:19
【问题描述】:
我在导入一系列模块并访问其中的类时遇到问题。
这是我的代码:
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]
有没有人知道为什么会发生这种情况,我已经导入了它,Menu 只是 Screen 中的一个类,如果需要我可以给你 Screen 类!
【问题讨论】:
-
@Blender 很遗憾我试过了,它说,
ImportError: No module named Menu -
在运行时修改
sys.path通常是一个错误。它使您的代码依赖于环境。对导入路径的任何更改都应在环境中进行,并且有许多系统可以做到这一点。
标签: python class import module