我有以下代码:
#!/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:~$
您的问题是您有一个名为 pygame.py
的文件,或者来自名为 pygame.pyc
的此类文件的剩余字节码。
调用import pygame
有效,但它不会导入 pygame,而是导入该文件。
由于您的文件在该文件中找不到名为 locals
的模块,因此会引发错误。
因此,只需将文件重命名为要导入的pygame.py
或其他模块以外的任何内容,或者,如果有pygame.pyc
字节码文件,请将其删除。
尝试使用 IDLE 而不是 IDLE 3。它对我有用,我遇到了上面显示的完全相同的问题。