我在導入一系列模塊和訪問類時遇到了問題。從python模塊中導入子類
這是我的代碼:
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]
有沒有人對爲什麼會這樣,我已經導入它和菜單任何想法,只是中的類屏幕,如果需要,我可以給你屏幕類!
'Import Screen.Menu' – Blender
@Blender很遺憾,我試過了,它說'ImportError:沒有名爲Menu的模塊' –
在運行時修改'sys.path'通常是一個錯誤。它使你的代碼環境相關。導入路徑的任何更改都應該在環境中進行,並且有很多系統可以執行此操作。 –