Pygame headless setup
Asked Answered
D

2

8

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))
Dice answered 2/10, 2015 at 0:28 Comment(0)
L
8

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()
Lawsuit answered 28/4, 2016 at 17:14 Comment(1)
thx. this worked for me os.environ["SDL_VIDEODRIVER"] = "dummy" pygame.display.init()Bostow
B
0

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.

Broadleaved answered 8/11, 2022 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.