valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Is it possible to run Pygame without startx?

Thu Jun 01, 2023 10:06 am

I made a Pygame program and it run's fine on desktop in Raspbian, but I would like to run it without desktop environment.
Is this possible?
When I tried this I got an error:
pygame.error: No available video device

pygame.display.info() returns:
pygame.error: video system not initialized

pygame.init() returrns:
(4, 1)

Running latest Raspberry Pi OS (64-bit) and Raspberry Pi 4 model B
Display is on hdmi
Last edited by valtsu23 on Thu Jun 01, 2023 12:03 pm, edited 1 time in total.

User avatar
bensimmo
Posts: 6290
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 11:14 am

In case you didn't know they have a nice website for pygame
https://www.pygame.org/

I think the answer is yes from the command line, but not sure of the limits there.
Best to read the readme/faq and forum/chat groups.
It's been a very long time since I looked at it though.

P. S. it is not Raspbian, especially if it's the 64bit OS. It's just RaspberryPi OS (RPiOS)

valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 12:02 pm

Thanks for the answer.
I have already tried to search the answer from pygame.org and Googling, but haven't found an answer. That's why I posted here.

User avatar
thagrol
Posts: 9858
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 12:06 pm

I've a feeling that pygame no longer runs without the desktop or, more specifically, without an X server.

You can always run startx some commane where "some command" is the command needed to run your pygame program. That gets you an X server running just your program without any of the usual dekstop stuff. It will drop back to the command line when your program exits.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
scruss
Posts: 5545
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 12:57 pm

While pygame relies on SDL, and it's possible to build SDL so it runs without X, it may be quite a slog to get it going
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 2:34 pm

If the environment is the same as "Boot to CLI" gives, which doesn't run 'startx' or launch the desktop AFAICT, my PyGame code runs from the command line, Pi 4B Bullseye 64-bit kernel, 32-bit user space, Python 3.9.2, PyGame 2.0.0, HDMI.

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 2:43 pm

bensimmo wrote:
Thu Jun 01, 2023 11:14 am
P. S. it is not Raspbian, especially if it's the 64bit OS. It's just RaspberryPi OS (RPiOS)
As long as 'cat /etc/os-release' and the rest keep reporting it as Raspbian one can't blame people for thinking that is what it's running, calling it Raspbian ...

Code: Select all

pi@Pi4B:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)"
NAME="Raspbian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Code: Select all

pi@Pi4B:~ $ cat /etc/issue
Raspbian GNU/Linux 11 \n \l

Code: Select all

pi@Pi4B:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 11 (bullseye)
Release:        11
Codename:       bullseye
That's my 4B running Raspberry Pi OS Bullseye, 64-bit kernel, 32-bit user space. I have no idea what 64-bit user space shows.

Code: Select all

Linux Pi4B 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64 GNU/Linux

User avatar
bensimmo
Posts: 6290
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 3:05 pm

No blame, just informing for the future. :-)


Many still call the 64bit Raspbian, worse on facebook as it perpetuates. 64bit says Debian as that is what it is based on.

valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 4:10 pm

hippy wrote:
Thu Jun 01, 2023 2:34 pm
If the environment is the same as "Boot to CLI" gives, which doesn't run 'startx' or launch the desktop AFAICT, my PyGame code runs from the command line, Pi 4B Bullseye 64-bit kernel, 32-bit user space, Python 3.9.2, PyGame 2.0.0, HDMI.
Interesting...
Can you share your code?

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 5:31 pm

This works for me, just tested. Should go full-screen, dull red, self-closes after 30 seconds ...

Code: Select all

import os
import pygame
import time

def FindDisplayDriver():
  for driver in ["fbcon", "directfb", "svgalib"]:
    if not os.getenv("SDL_VIDEODRIVER"):
      os.putenv("SDL_VIDEODRIVER", driver)
    try:
      pygame.display.init()
      return True
    except pygame.error:
      pass
  return False

if __name__ == "__main__":
  pygame.init()
  if not FindDisplayDriver():
    print("Failed to initialise display driver")
  else:
    pygame.mouse.set_visible(False)
    width  = pygame.display.Info().current_w
    height = pygame.display.Info().current_h
    screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
    screen.fill((0x3F, 0, 0))
    pygame.display.update()
    time.sleep(30)
    pygame.quit()
Added : I have just discovered some weirdness with PyGame installations. If you could launch Python3 from the command line, enter the REPL, type "import pygame", "import pygame.font", then "exit()" or Ctrl-D to exit, that would help me and might also explain why it might be working for me but not for you if that is the case -

Code: Select all

pi@Pi4B:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.0 (SDL 2.0.14, python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> import pygame.font
>>> exit()
pi@Pi4B:~ $
If others want to play along and report what they get that is fine by me, and could help me figure some things out.

jalih
Posts: 260
Joined: Mon Apr 15, 2019 3:54 pm

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 7:17 pm

hippy wrote:
Thu Jun 01, 2023 5:31 pm

Code: Select all

def FindDisplayDriver():
  for driver in ["fbcon", "directfb", "svgalib"]:
    if not os.getenv("SDL_VIDEODRIVER"):
      os.putenv("SDL_VIDEODRIVER", driver)
    try:
      pygame.display.init()
      return True
    except pygame.error:
      pass
  return False
Why no "kmsdrm" video driver? I use Nuklear based GUI using SDL2 backend with the 8th programming language and performance is very good without the X11!

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Thu Jun 01, 2023 8:40 pm

jalih wrote:
Thu Jun 01, 2023 7:17 pm
Why no "kmsdrm" video driver?
Because I have never needed it. Imagine there are a whole number of things which could be included in my list which aren't there as I never needed them or I wasn't aware of them.

valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Re: Is it possible to run Pygame without startx?

Fri Jun 02, 2023 10:09 am

hippy wrote:
Thu Jun 01, 2023 5:31 pm
This works for me, just tested. Should go full-screen, dull red, self-closes after 30 seconds ...

Code: Select all

import os
import pygame
import time

def FindDisplayDriver():
  for driver in ["fbcon", "directfb", "svgalib"]:
    if not os.getenv("SDL_VIDEODRIVER"):
      os.putenv("SDL_VIDEODRIVER", driver)
    try:
      pygame.display.init()
      return True
    except pygame.error:
      pass
  return False

if __name__ == "__main__":
  pygame.init()
  if not FindDisplayDriver():
    print("Failed to initialise display driver")
  else:
    pygame.mouse.set_visible(False)
    width  = pygame.display.Info().current_w
    height = pygame.display.Info().current_h
    screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
    screen.fill((0x3F, 0, 0))
    pygame.display.update()
    time.sleep(30)
    pygame.quit()
Added : I have just discovered some weirdness with PyGame installations. If you could launch Python3 from the command line, enter the REPL, type "import pygame", "import pygame.font", then "exit()" or Ctrl-D to exit, that would help me and might also explain why it might be working for me but not for you if that is the case -

Code: Select all

pi@Pi4B:~ $ python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.0 (SDL 2.0.14, python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> import pygame.font
>>> exit()
pi@Pi4B:~ $
If others want to play along and report what they get that is fine by me, and could help me figure some things out.
Here is what I got.

Code: Select all

>>> import pygame
pygame 2.1.2 (SDL 2.0.16, python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> import pygame.font
>>> exit()
I tried your example code:
- Works in desktop environment
- Not from command line
Error message: Failed to initialise display driver

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Fri Jun 02, 2023 12:19 pm

valtsu23 wrote:
Fri Jun 02, 2023 10:09 am
I tried your example code:
- Works in desktop environment
- Not from command line
Error message: Failed to initialise display driver
Thanks, and for your Python test.

It appears that running under the desktop, running from a command line opened from the desktop, running from a command line when 'Boot to CLI' has been selected, is different to running from a command line of a Raspberry Pi OS Lite installation -

https://github.com/pygame/pygame/issues/3168

First thing I would suggest is trying 'sudo' and keeping your fingers crossed, adding "kmsdrm" which, with hindsight, may be what 'jalih' was suggesting.

The OP of the linked issue threw in the towel, reverted to Buster and an older version of PyGame which appears to have worked for them. I would take the path of installing the non-Lite, desktop without recommended software, OS and booting to CLI. Ignoring that it's a larger install with not necessarily needed stuff if it solves the issue.

I expect you don't really want to do either but "sub-par but working" is a better position to be in than "would be perfect but doesn't work".

I don't have a Bullseye Lite install and I have little understanding of the video side of things since everything changed for the Pi 4B and/or Bullseye so can't really help further.

valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Re: Is it possible to run Pygame without startx?

Fri Jun 02, 2023 4:41 pm

Thanks for your input.
I'll look at this when I have time.

Project goal was to start Pygame program quickly as possible from boot.
It's a gauge cluster for a car, so startup speed is a good thing.

Right now starting to desktop and automatically starting the program takes about 40 seconds. Wich isn't bad, but could be better.

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Fri Jun 02, 2023 5:58 pm

valtsu23 wrote:
Fri Jun 02, 2023 4:41 pm
Project goal was to start Pygame program quickly as possible from boot.
It's a gauge cluster for a car, so startup speed is a good thing.

Right now starting to desktop and automatically starting the program takes about 40 seconds. Wich isn't bad, but could be better.
I would have said 40 seconds was terrible. I expect my instruments to work from the moment I turn the ignition key.

I would suggest you need to start it booting as soon as a door is opened, so it's ready for the ignition to be switched on, and find a faster way of booting.

Raspberry Pi OS, designed as it is to be a general purpose OS, is probably the wrong tool for the job. You might be able to strip it down, use some buildroot, use an alternative OS, or go bare metal, Ultibo perhaps.

I would also suggest you may be better off not using PyGame but writing directly to a frame buffer - which is all you are likely doing with whole layers of abstraction between your Python program and that frame buffer.

It might be worth looking to see if a Pico would better suit your needs. That boots almost immediately, draws less power, and supports MicroPython if you want to go that route.

valtsu23
Posts: 12
Joined: Mon Feb 14, 2022 4:46 pm

Re: Is it possible to run Pygame without startx?

Sat Jun 03, 2023 8:36 am

Yes there is lots of things to improve. My knowledge on Linux and Raspberry Pi is quite limited.
This project is more like coding training for me.

I have better knowledge from microcontrollers like Arduino and Adafruits CircuitPython, than Raspberry Pi.
Problem with smaller microcontrollers is performance on bigger screens.
I'm using 5" 800x480 display. Also display needs to be readable on daylight and those displays are hard to find.
I managed to find Raspberry Pi compatible display that filled my needs and went with that.
https://store.makerplane.org/5-sunlight ... pberry-pi/

Thanks for your input!

bigbohemia
Posts: 3
Joined: Wed Oct 14, 2020 12:50 am

Re: Is it possible to run Pygame without startx?

Tue Sep 12, 2023 4:14 pm

Do you have code that you can share? I am also working on an auto gauge cluster imaged by pygame, and pygame 2.x does not work for me on Raspberry Pi OS. I had to use pygame 1.9.6, which should come installed on the OS. Before, I was able to install python in a virtual environment, but now, I have to install all my dependencies into the global python3 executable, and I run using:

python3 /path/to/my/directory/runClient.py

This is my Github that I'm running this:

https://github.com/lmandres/GT06ScannerClient

EDIT: Wrong github repo:

https://github.com/lmandres/Python-OBD-Scanner

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Tue Sep 12, 2023 6:22 pm

bigbohemia wrote:
Tue Sep 12, 2023 4:14 pm
I am also working on an auto gauge cluster imaged by pygame, and pygame 2.x does not work for me on Raspberry Pi OS.
In what way did it not work ?

If it's missing modules you might solve that with, or something similar -

sudo apt-get install libsdl2-mixer-2.0-0 libsdl2-image-2.0-0 libsdl2-2.0-0 libsdl2-ttf-2.0-0

viewtopic.php?t=291180

bigbohemia
Posts: 3
Joined: Wed Oct 14, 2020 12:50 am

Re: Is it possible to run Pygame without startx?

Tue Sep 12, 2023 7:15 pm

I think my last post was incomplete. I was trying to run pygame in the command line without a window system. I have been able to bring up my application in pygame 1.9.6 without having to load a desktop, and with Raspberry Pi OS 64 bit, I have to use the default installation of pygame, which is 1.9.6.

EDIT: Also, I tried installing those dependencies, and it still didn't work in the command line.

hippy
Posts: 14361
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Is it possible to run Pygame without startx?

Tue Sep 12, 2023 8:24 pm

Thanks for the clarification. What error message are you getting ?

Return to “Graphics programming”