PYGame - ImportError:没有名为locals Raspberry Pi的模块

时间:2012-08-14 09:13:45

标签: python pygame raspberry-pi

我有以下代码:

 #!/usr/bin/python
 # -*- coding: iso-8859-15 -*-

 import pygame, random
 from pygame.locals import *
 pygame.init()
 clock = pygame.time.Clock()

 and so on

应用程序全部显示正常,但是当我编译代码时出现以下错误:

 Traceback (most recent call last):
 File "fish.py", line 4, in <module>
import pygame, random
 File "/home/pi/pygame/pygame.py", line 2, in <module>
ImportError: No module named locals


------------------
(program exited with code: 1)
Press return to continue

有人可以帮忙吗?我是Python和Linux的新手。

我做了以下事情:

 pi@raspberrypi:~$ sudo apt-get install python-pygame
 Reading package lists... Done
 Building dependency tree       
 Reading state information... Done
 python-pygame is already the newest version.
 0 upgraded, 0 newly installed, 0 to remove and 40 not upgraded.
 pi@raspberrypi:~$ 

2 个答案:

答案 0 :(得分:6)

您的问题是您有一个名为pygame.py的文件,或者一个名为pygame.pyc的文件留下的字节码。


调用import pygame有效,但它不会导入pygame,而是导入该文件。

由于您的文件无法在该文件中找到名为locals的模块,因此会出现错误。

因此,只需将您的文件重命名为pygame.py或您要导入的其他模块,或者,如果有pygame.pyc字节码文件,请将其删除。

答案 1 :(得分:0)

尝试使用IDLE而不是IDLE 3.它对我有用,而且上面显示了完全相同的问题。