[英]Python: Cannot use module inside package
我在模塊中有一些代碼
import pygame
from pygame.locals import *
import reusable
from state import state
class TitleState(state.State):
def __init__(self):
#Create data batches
self.soundbatch = reusable.resourceloader.ResourceBatch(path="data/sound", type="ogg")
self.imagebatch = reusable.resourceloader.ResourceBatch(path="data/image", type="tga")
#Reusable stuff
self.inputengine = reusable.inputengine.InputEngine()
self.mousehandler = reusable.mousehandler.MouseHandler()
def update(self):
pygame.event.pump()
但這給了我錯誤
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\MegaCity\Start.py", line 1, in <module>
import megacity.megacity
File "C:\Users\Administrator\Desktop\MegaCity\megacity\megacity.py", line 37, in <module>
themc = MegaCity()
File "C:\Users\Administrator\Desktop\MegaCity\megacity\megacity.py", line 25, in __init__
self.titlestate = titlestate.TitleState()
File "C:\Users\Administrator\Desktop\MegaCity\state\titlestate.py", line 21, in __init__
self.inputengine = reusable.inputengine.InputEngine()
AttributeError: 'module' object has no attribute 'inputengine'
但是,可重用目錄肯定在其中包含“ inputengine”:
Directory of C:\Users\Administrator\Desktop\MegaCity\reusable
10/09/2012 05:29 PM <DIR> .
10/09/2012 05:29 PM <DIR> ..
10/08/2012 09:34 PM 3,920 inputengine.py
10/09/2012 04:54 PM 1,364 mousehandler.py
10/08/2012 09:42 PM 799 resourceloader.py
10/09/2012 05:32 PM 2 __init__.py
10/09/2012 05:32 PM <DIR> __pycache__
但是,當我from reusable import inputengine
它進口inputengine
就好了。 另外,當在__init__.py
可重用時,我from . import inputengine
放from . import inputengine
from . import inputengine
代碼即可。 有什么見解嗎?
它應該是這樣工作的(不是錯誤,而是功能)。
您必須在導入中包含絕對導入的軟件包名稱,如果您不想這樣做,則可以使用發現的相對導入(即from . import inputengine
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.