Skip to main content
 首页 » 编程设计

python之pygame 项目无法运行 : "pygame.error: No available video device"

2025年05月04日16me-sa

更新 1:我卸载、重新安装并按照 Pygame 网站上的说明安装依赖项/构建 Pygame。事实证明我缺少依赖项,正如 Python 在我尝试运行 setup.py 脚本时指示的那样:

SDL     : found 1.2.15 
FONT    : not found 
IMAGE   : not found 
MIXER   : not found 
SMPEG   : found 0.4.5 
PNG     : not found 
JPEG    : not found 
SCRAP   : not found 
PORTMIDI: found 
PORTTIME: found 

我在别处有一份 list ,列出了与丢失的包裹相对应的内容。我的问题是:我需要修改什么环境变量才能检测到这些包? (而且,这样做时,我需要提供什么?每个共享目标文件?)

编辑:系统详细信息 - Linux Mint 17.2 Cinnamon 64 位。肉桂版本:2.6.13。显卡:Intel Corporation Broadwell-U Integrated Graphics。 7.7 GiB 内存,i5 处理器。

我正在尝试在我的新设备上启动并运行 pygame 来制作一些游戏。我最近安装了 libSDL 和 pygame;在尝试运行其他人制作的三种不同游戏后,我一直遇到同样的错误:

>~/Downloads/bubble $ python run.py 
Traceback (most recent call last): 
File "run.py", line 2, in <module> 
   from classes.game import * 
File "/home/---/Downloads/bubble/classes/game.py", line 2, in <module> 
   from .arrow import * 
File "/home/---/Downloads/bubble/classes/arrow.py", line 1, in <module> 
   from .constantes import * 
File "/home/---/Downloads/bubble/classes/constantes.py", line 35, in <module> 
scr = display.set_mode(screen.size) 
pygame.error: No available video device 

我为调查问题/尝试解决问题所做的工作:

1) 仔细检查我是否安装了 libSDL 和相应的开发包。 [经过研究,导致此错误的原因之一是没有安装 libSDL]

>~aptitude search sdl | grep -F 'i ' 
 
i A libalien-sdl-perl               - helper to get, build and use SDL libraries 
i A libsdl-console                  - Console that can be added to any SDL appli 
i   libsdl-console-dev              - Console that can be added to any SDL appli 
i A libsdl-gfx1.2-4                 - drawing and graphical effects extension fo 
i   libsdl-gst                      - SDL bindings for GNU Smalltalk             
i   libsdl-image-gst                - SDL_image bindings for GNU Smalltalk       
i   libsdl-image1.2                 - Image loading library for Simple DirectMed 
i   libsdl-image1.2:i386            - Image loading library for Simple DirectMed 
i A libsdl-image1.2-dev             - Image loading library for Simple DirectMed 
i   libsdl-mixer-gst                - SDL_mixer bindings for GNU Smalltalk       
i A libsdl-mixer1.2                 - Mixer library for Simple DirectMedia Layer 
i   libsdl-mixer1.2:i386            - Mixer library for Simple DirectMedia Layer 
i A libsdl-net1.2                   - Network library for Simple DirectMedia Lay 
i   libsdl-net1.2:i386              - Network library for Simple DirectMedia Lay 
i   libsdl-net1.2-dbg               - Network library for Simple DirectMedia Lay 
i   libsdl-net1.2-dev               - Network library for Simple DirectMedia Lay 
i A libsdl-pango1                   - text rendering with Pango in SDL applicati 
i   libsdl-perl                     - SDL bindings for the Perl language         
i A libsdl-sge                      - extension of graphic functions for SDL mul 
i   libsdl-sge-dev                  - extension of graphic functions for the SDL 
i   libsdl-sound-gst                - SDL_sound bindings for GNU Smalltalk       
i A libsdl-sound1.2                 - Sound library for Simple DirectMedia Layer 
i   libsdl-ttf-gst                  - SDL_ttf bindings for GNU Smalltalk         
i A libsdl-ttf2.0-0                 - TrueType Font library for Simple DirectMed 
i   libsdl-ttf2.0-0:i386            - TrueType Font library for Simple DirectMed 
i   libsdl-ttf2.0-dev               - TrueType Font library for Simple DirectMed 
i A libsdl1.2-dev                   - Simple DirectMedia Layer development files 
i   libsdl1.2debian                 - Simple DirectMedia Layer                   
i   libsdl1.2debian:i386            - Simple DirectMedia Layer                   
i A libsdl2-2.0-0                   - Simple DirectMedia Layer                   
i   libsdl2-dbg                     - Simple DirectMedia Layer debug files       
i   libsdl2-dev                     - Simple DirectMedia Layer development files 
i   libsdl2-gfx-1.0-0               - drawing and graphical effects extension fo 
i   libsdl2-gfx-dbg                 - debugging symbols for SDL2_gfx             
i   libsdl2-gfx-dev                 - development files for SDL2_gfx             
i   libsdl2-gfx-doc                 - documentation files for SDL2_gfx           
i   libsdl2-image-2.0-0             - Image loading library for Simple DirectMed 
i   libsdl2-image-dbg               - Image loading library for Simple DirectMed 
i   libsdl2-image-dev               - Image loading library for Simple DirectMed 
i   libsdl2-mixer-2.0-0             - Mixer library for Simple DirectMedia Layer 
i   libsdl2-mixer-dbg               - Mixer library for Simple DirectMedia Layer 
i   libsdl2-mixer-dev               - Mixer library for Simple DirectMedia Layer 
i   libsdl2-net-2.0-0               - Network library for Simple DirectMedia Lay 
i   libsdl2-net-dbg                 - Network library for Simple DirectMedia Lay 
i   libsdl2-net-dev                 - Network library for Simple DirectMedia Lay 
i   libsdl2-ttf-2.0-0               - TrueType Font library for Simple DirectMed 
i   libsdl2-ttf-dbg                 - TrueType Font library for Simple DirectMed 
i   libsdl2-ttf-dev                 - TrueType Font library for Simple DirectMed 

2) 确保我为 SDL_VIDEODRIVER 设置了合适的环境变量。 [研究了另一个帖子,提示需要设置这个变量,否则pygame无法正常运行]

>~set | grep 'SDL' 
SDL_VIDEODRIVER=x11 

[补充一下前者:我运行了一个简单的已编译 .c 文件,演示了一个在 libSDL 中渲染了图像的窗口,它工作得很好!]

在仔细检查安装了 libSDL/设置了驱动程序的 envvar 之后,对于我从 pygame 站点下载的三个不同游戏,这个特定错误仍然存​​在。在这一点上我很困惑,如果能帮助解决这个问题并让 pygame 工作,我将不胜感激。非常感谢您的宝贵时间。

请您参考如下方法:

这是来自问题;它已被移至此处以进一步揭示答案。


好的。我让 pygame 在我的 Linux 安装上工作。由于不相关的原因,我不得不重新分区,但我想这样做之后我在新安装中遵循了这些步骤,并最终让它工作:(链接描述下面的头文件问题,libv4l-dev)

  1. 通过在命令行中输入以下内容来安装所有依赖项: ~> apt-get install libsdl1.2-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev
  2. 运行 SDL 配置命令: ~> sdl-config --cflags --libs
  3. 进入 pygame-1.9.1release 文件夹,然后运行: ~> python config.py 查看我缺少的依赖项(如果有的话)。 此时,我缺少:SMPEG、PORTMIDI 和 PORTTIME。 我只是查看了这些并使用下载了它们 Synaptic 包管理器。
  4. 之后再次运行 python config.py 显示我有 所有必需的依赖项。
  5. 然后我运行 sudo python setup.py install 并遇到了一些错误 关于找不到头文件。找到另一个 StackExchange 帖子详细说明了如何解决此特定问题: ~> sudo apt-get install libv4l-dev ~> cd/usr/include/linux ~> sudo ln -s ../libv4l1-videodev.h videodev.h
  6. 再次运行 sudo python setup.py,它成功了。 通过运行 python 确认这一点,然后检查版本 import pygame 然后是 pygame.version.ver()... 并快速运行别人制作的游戏。