首页 > 解决方案 > 如何修复 docker 上与 pygame 相关的“pygame.error:没有可用的视频设备”错误?

问题描述

我正在尝试使用 pygame 脚本运行图像,但出现错误: pygame.error: No available video device

pygame 脚本在我的计算机上运行良好(Linux、Ubuntu 16.04、32 位,我使用以下步骤安装了 docker:https : //stackoverflow.com/a/48881019/6796652),当我将它包含在 dockerfile 图像中时会出现问题.

我阅读了一些相关的帖子,因为这个pygame 项目将无法运行:“pygame.error: No available video device”,但我在 dockerfile 中找不到相同的问题。

这是 pygame 脚本的一部分:

import pygame

def main():
    pygame.init()
    screen=pygame.display.set_mode((400,600))

    # Here are more code...
    # And finally

    pygame.quit()

main() 

我尝试包括这部分,因为这篇文章说https://stackoverflow.com/a/53623914/6796652

from pygame.locals import *

这是我的 Dockerfile:

FROM python:3
WORKDIR /myfolder
COPY main.py someimage.png wallpaper.jpg requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./main.py" ]

我也尝试使用 Ubuntu 映像,但结果完全相同:

FROM ubuntu:14.04
WORKDIR /myfolder
COPY main.py someimage.png wallpaper.jpg requirements.txt ./
RUN apt-get update
RUN apt-get install python-pip python-dev build-essential -y
RUN pip install --upgrade pip 
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./main.py" ]

甚至,如文档所述,安装 Python 3.x 的依赖项:http: //www.pygame.org/wiki/CompileUbuntu结果是一样的

FROM python:3
WORKDIR /DefeatTheBat
COPY bat.png main.py wallpaper.jpg requirements.txt ./
RUN apt-get update && apt-get install -y \
    python3-dev -y \ 
    python3-setuptools -y \ 
    python3-numpy -y \ 
    python3-opengl -y  \ 
    libsdl-image1.2-dev -y \ 
    libsdl-mixer1.2-dev -y \ 
    libsdl-ttf2.0-dev -y \ 
    libsmpeg-dev -y \ 
    libsdl1.2-dev -y \ 
    libportmidi-dev -y \ 
    libswscale-dev -y \ 
    libavformat-dev -y \ 
    libavcodec-dev -y \ 
    libtiff5-dev -y \ 
    libx11-6 -y \ 
    libx11-dev -y \ 
    fluid-soundfont-gm -y \ 
    timgm6mb-soundfont -y \ 
    xfonts-base -y \ 
    xfonts-100dpi -y \ 
    xfonts-75dpi -y \ 
    xfonts-cyrillic -y \ 
    fontconfig -y \ 
    fonts-freefont-ttf -y \ 
    libfreetype6-dev -y
RUN pip install --no-cache-dir -r requirements.txt
CMD [ "python", "./main.py" ]

还有requirements.txt:

pygame==1.9.5

我收到此错误:

    screen=pygame.display.set_mode((400,600)) 
pygame.error: No available video device

什么时候应该显示窗口。如果我在我的交互式 python shell 中尝试该部分,它工作得很好。

我该如何修复这个 dockerfile 图像?

标签: python-3.xdockerpygamedockerfile

解决方案


推荐阅读