Python 说 pygames 没有属性“init()”;但lib安装正确
我正在使用 Python 2.7
和来自 http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
我运行了这个脚本:
import pygame
print 'PyGame Version: %s ' % pygame.__version__
看看如果我的模块安装正确并且我得到:
PyGame Version: 1.9.2pre
据我所知,这意味着 pygame 工作正常。在Python IDLE中,我尝试以交互方式运行:
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")
并且它工作正常。但每当我尝试将其作为脚本运行时,我都会得到:
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'
On Eclipse 或者 Running in IDLE。这可能是什么?
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
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'
On Eclipse or Running in IDLE either. What this could be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的脚本名为 pygame.py,Python 将导入它而不是实际的 pygame 包(当您执行 import pygame 时)。这可能可以解释你的错误。
If your script is named
pygame.py
Python will import it instead of the actualpygame
package (when you doimport pygame
). That might explain your error.当您将脚本命名为 pygame.py 并尝试运行它时,python 创建了一个文件名“pygame.pyc”(=python 编译文件)并将其放在您保存 pygame.py 脚本的目录中。
您必须删除新创建的 .pyc 文件并将脚本重命名为 my_py_game.py 而不是 pygame.py
when you named your script pygame.py and tried to run it, python created a file name "pygame.pyc" (=python compiled file) and put it in the directory you saved your pygame.py script.
you have to remove the newley created .pyc file and rename your script to somthing like my_py_game.py instead of pygame.py