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
Is it possible to run Pygame without startx?
Last edited by valtsu23 on Thu Jun 01, 2023 12:03 pm, edited 1 time in total.
Re: Is it possible to run Pygame without startx?
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)
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)
Re: Is it possible to run Pygame without startx?
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.
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.
Re: Is it possible to run Pygame without startx?
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.
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
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
Re: Is it possible to run Pygame without startx?
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
Pronouns: he/him
Re: Is it possible to run Pygame without startx?
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.
Re: Is it possible to run Pygame without startx?
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
Code: Select all
Linux Pi4B 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64 GNU/Linux
Re: Is it possible to run Pygame without startx?
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.

Many still call the 64bit Raspbian, worse on facebook as it perpetuates. 64bit says Debian as that is what it is based on.
Re: Is it possible to run Pygame without startx?
Interesting...
Can you share your code?
Re: Is it possible to run Pygame without startx?
This works for me, just tested. Should go full-screen, dull red, self-closes after 30 seconds ...
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 -
If others want to play along and report what they get that is fine by me, and could help me figure some things out.
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()
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:~ $
Re: Is it possible to run Pygame without startx?
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 wrote: ↑Thu Jun 01, 2023 5:31 pmCode: 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
Re: Is it possible to run Pygame without startx?
Here is what I got.hippy wrote: ↑Thu Jun 01, 2023 5:31 pmThis works for me, just tested. Should go full-screen, dull red, self-closes after 30 seconds ...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
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()
If others want to play along and report what they get that is fine by me, and could help me figure some things out.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:~ $
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()
- Works in desktop environment
- Not from command line
Error message: Failed to initialise display driver
Re: Is it possible to run Pygame without startx?
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.
Re: Is it possible to run Pygame without startx?
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.
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.
Re: Is it possible to run Pygame without startx?
I would have said 40 seconds was terrible. I expect my instruments to work from the moment I turn the ignition key.valtsu23 wrote: ↑Fri Jun 02, 2023 4:41 pmProject 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 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.
Re: Is it possible to run Pygame without startx?
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!
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!
-
- Posts: 3
- Joined: Wed Oct 14, 2020 12:50 am
Re: Is it possible to run Pygame without startx?
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
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
Re: Is it possible to run Pygame without startx?
In what way did it not work ?bigbohemia wrote: ↑Tue Sep 12, 2023 4:14 pmI am also working on an auto gauge cluster imaged by pygame, and pygame 2.x does not work for me on Raspberry Pi OS.
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
-
- Posts: 3
- Joined: Wed Oct 14, 2020 12:50 am
Re: Is it possible to run Pygame without startx?
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.
EDIT: Also, I tried installing those dependencies, and it still didn't work in the command line.
Re: Is it possible to run Pygame without startx?
Thanks for the clarification. What error message are you getting ?