在python3中安装pygame库,(如果没有pip3命令使用 sudo apt-get install python3-pip 安装。)
$ sudo pip3 install pygame
遇到如下问题
Downloading/unpacking pygame Downloading pygame-1.9.1release.tar.gz (2.1MB): 2.1MB downloaded Running setup.py egg_info for package pygame WARNING, No "Setup" File Exists, Running "config.py" Using UNIX configuration... /bin/sh: 1: sdl-config: not found /bin/sh: 1: smpeg-config: not found Hunting dependencies... WARNING: "sdl-config" failed! WARNING: "smpeg-config" failed! Unable to run "sdl-config". Please make sure a development version of SDL is installed. No files/directories in /tmp/pip-build-root/pygame/pip-egg-info (from PKG-INFO) Storing complete log in /home/david/.pip/pip.log
目测是没有安装依赖库,然后执行如下命令后
$ sudo apt install apt-file
$ apt-file update
$ sudo apt install libsdl1.2-dev
然后再执行上面的安装的命令,发现依赖更多库了,如下图所示,
然后,我找到了一个野蛮的解决方案,见博客:https://blog.csdn.net/zkwniky/article/details/54768739
以下表格内容引用该博客
#如果是python 3.X #install dependencies sudo apt-get install mercurial python3-dev python3-numpy ffmpeg libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev # Grab source hg clone https://bitbucket.org/pygame/pygame # Finally build and install cd pygame python3 setup.py build sudo python3 setup.py install |
执行验证命令
$ python3 -m pygame.examples.aliens
终于看到了pygame自带的游戏界面了,说明pygame库安装成功了,本来故事应该在此刻结束了,但是打开PyCharm编程时发现无法import pygame,说这个库找不到,去查看pygame模块的安装目录,也在python3的安装目录下,查了下也好多人遇到这个问题,猜测可能是pygame的路径未添加到PyCharm中,所以查找PyCharm如何添加新库,然后发现了新大陆。
原来PyCharm支持直接添加pygame库,方法如下:
1. 菜单: file --> sittings,得窗口如下:
2. 点击右边的“+”得如下界面
3. 过一段时间后,可见安装成功了,就可以import pygame使用了