Pygame headless setup

Question:

I am using pygame’s joystick api to use a joystick with my project on a headless system, but pygame requires a “screen” so I have setup a dummy video system to over come this. It worked fine but now all of a sudden it gives me this error:

Traceback (most recent call last):
  File "compact.py", line 10, in <module>
    screen = display.set_mode((1, 1))
pygame.error: Unable to open a console terminal

Here is what I have as the headless setup that is supposed to over come this issue.

from pygame import *
import os
import RPi.GPIO as GPIO
os.environ["SDL_VIDEODRIVER"] = "dummy"
screen = display.set_mode((1, 1))
Asked By: DSchana

||

Answers:

Pygame is trying to open a console which means you’re running this script through ssh or cron or somewhere else that doesn’t have access to the console. I would try skipping set_mode (since the dummy driver likely doesn’t have modes to set) and just try to initialize the display. You can try running it as root which might give it access. You can also try telling it to use fbcon.

os.putenv('SDL_VIDEODRIVER', 'fbcon')
pygame.display.init()
Answered By: John Hundley

If you don’t have an actual monitor connected to the Raspberry Pi, pygame.display will not work. However, there are 2 ways to trick the system into creating a virtual display so that you can run Pygame without a monitor:

  1. You can trick the raspberry pi into thinking there is a monitor attached by using a hdmi emulator dummy plug: enter image description here

  2. Alternatively, go to /boot folder, edit the config.txt file with sudo or root access, set:

hdmi_force_hotplug=1 (without the #)

And the pi will create a virtual display even if there is no actual monitor attached.

Answered By: user3528623
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.