导入时出错 pygame

Error while import pygame

我使用以下命令安装了 pygame 和 Anaconda:
conda install --channel https://conda.anaconda.org/kne pygame

然后我尝试导入 pygame,但出现以下错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/__init__.py", line 133, in <module>
from pygame.base import *
ImportError: dlopen(/Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so, 2): Library not loaded: /usr/local/opt/sdl/lib/libSDL-1.2.0.dylib
Referenced from: /Users/ejalaa/anaconda/lib/python2.7/site-packages/pygame/base.so
Reason: image not found

这是什么意思? 知道我该如何解决吗?

谢谢。

一个。检查您使用的是 64 位还是 32 位版本 pygame 并确保您的 python 版本相同。

乙。使用类似 pip install 或任何其他类型的方法来安装 pygame.

C。安装以前版本的 pygame 和 python,你可能会更幸运。

错误基本上是说找不到 pygame 中名为图像的 class。很明显pygame没有安装好所以建议用上面的方法重新安装

希望对您有所帮助!

浪费了 2 小时后,我意识到罪魁祸首是导致安装错误的 "conda install"。

解决办法就是卸载它然后 pip 安装它:

pip uninstall pygame pip install pygame

这里有类似的问题。

我的经验:使用conda install -c https://conda.anaconda.org/quasiben pygame安装1.9.1。

然后conda uninstall pygame,然后pip install pygame(这次是1.9.3,成功了)

如果您使用 python 2.7,mac os 和 conda 进行包管理。你应该试试这个。 (我只对 python 2.7 进行了测试,但类似的方法也适用于 python 3.x)

[可选]如果您还没有创建 conda 环境,您可以创建一个,如下所示。假设您的 conda 环境名称 'conda-env-p27'

conda create -n conda-env-p27 python=2.7 anaconda

激活您的 conda 环境。假设您的 conda 环境名称 'conda-env-p27'

source activate conda-env

[可选] 如果您已经安装了 pygame with conda,则必须将其卸载。

conda uninstall pygame

然后,使用 pip 包管理器安装 pygame。 ()

pip install pygame

我在激活 conda 环境之前安装了 pygame。但是,它不起作用。但是,在激活 conda 环境之后,我用 pip 再次安装了它,它对我有用。