Python 说 pygames 没有属性 'init()' 但 lib 安装正确 [英] Python says pygames has no attribute 'init()' but lib is installed correctly
问题描述
我使用 Python 2.7
和 pygame-1.9.2pre.win32-py2.7.exe
来自 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
I'm using Python 2.7
with pygame-1.9.2pre.win32-py2.7.exe
from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
我已经运行了这个脚本:
I've run this script:
import pygame
print 'PyGame Version: %s ' % pygame.__version__
看看我的模块是否安装正确,我得到了:
to see if my module was installed correctly and I got:
PyGame Version: 1.9.2pre
据我所知,这意味着 pygame 工作正常.在 Python IDLE 中,我尝试以交互方式运行:
As far as I know, it means that pygame is working correctly. In Python IDLE i try to run interactively:
import pygame
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
而且它工作正常.但是每当我尝试将其作为脚本运行时,我都会得到:
And it works fine. But whenever I try to run this as a script I get:
Traceback (most recent call last):
File "D:\Computing\Workspaces\Python\test\pygame.py", line 5, in <module>
import pygame
File "D:\Computing\Workspaces\Python\test\pygame.py", line 13, in <module>
pygame.init()
AttributeError: 'module' object has no attribute 'init'
在 Eclipse 上或在 IDLE 中运行.这可能是什么?
On Eclipse or Running in IDLE either. What this could be?
推荐答案
如果您的脚本名为 pygame.py
Python 将导入它而不是实际的 pygame
包(当您执行 import pygame
时).这可能可以解释您的错误.
If your script is named pygame.py
Python will import it instead of the actual pygame
package (when you do import pygame
). That might explain your error.
这篇关于Python 说 pygames 没有属性 'init()' 但 lib 安装正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!