2

I am trying to create a python script with pygame to control servos with a gamepad. I want to be able to do this without a display of any kind. I am running the script on a Raspberry Pi 4 Model B with Raspbian OS and to run scripts I am using SSH from bash terminal (I've tried on VS Code terminal and git-bash terminal) on a windows computer with OS Windows 8. I've seen some work arounds for headless pygame code:

https://www.pygame.org/wiki/HeadlessNoWindowsNeeded

https://gist.github.com/illume/f2fcdde9478aa3f1db8f3ee17359cf1c

Pygame headless setup

Pygame display init on headless Raspberry Pi Zero (Raspbian Buster Lite)

Pygame on Pi running through Putty, no screen, no input

Pygame on Pi running through Putty, no screen, no input

However, none have worked and I keep getting these errors:

Traceback (most recent call last):
  File "ps4ServoControl.py", line 15, in <module>
    pygame.display.init()
pygame.error: Unable to open a console terminal

or

Traceback (most recent call last):
  File "ps4ServoControl.py", line 63, in <module>
    for event in pygame.event.get():
pygame.error: video system not initialized

I've tried every variation of the following lines:

os.environ["SDL_VIDEODRIVER"] = "dummy"
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('DISPLAY', ':0.0')
pygame.display.init()
screen = pygame.display.set_mode((1,1))

Is it just because I'm trying to do this with SSH? I'm not sure how else to test my scripts without doing a remote desktop, but again, I don't want to have to pull up a screen somewhere when I get the script working. Any help is appreciated. Thanks!

Parker
  • 21
  • 3
  • If I'm not mistaken, pygame is built on the premise that you have a screen. I believe the event system requires a screen (hence the `pygame.error: video system not initialized`, which means you haven't initialized the screen). At least when I last saw this type of question, it wasn't possible to use pygame without it. – Ted Klein Bergman Nov 19 '20 at 14:36
  • @TedKleinBergman - It was definitely possible last year. I was using it for some sound stuff, but after updating everything, I'm crashing. – Eric G Nov 29 '20 at 23:05

1 Answers1

0

In my case, I was headless and using pygame for sounds. The code you showed used to work for me, but after updating this year I was getting the same error. I resolved it by commenting out the following lines:

pygame.display.init()
screen = pygame.display.set_mode((1,1))

Everything seems to be fine without them.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Eric G
  • 3,427
  • 5
  • 28
  • 52