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!
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.
Related
I got a problem with setting up my Pygame code on RasPi 4. I have HDMI display (7" for now, later to be replaced by 4" on final product) connected to it (boot to console), but do all development on remote desktop.
When I use pygame 1.9, i have no problems using fbcon driver, and can show stuff on display. I now installed pygame_gui, and it pushed up the pygame version to 2.1.2. Now, I cant get the screen working, the error is
pygame 2.1.2 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Setting SDL variables...
...done
Checking current env variables...
SDL_FBDEV = /dev/fb0
Traceback (most recent call last):
File "/home/proto/pygameui.py", line 24, in <module>
pygame.display.init()
pygame.error: fbcon not available
I tried to set driver to fbcon, directfb, svgalib, kmsdrm, but in vain.
Trying to downgrade pygame and pygame_gui to 1.9 and 5.7 worked for normal pygame graphics, but I cant get gui stuff to work with this version..
Any idea what to do?
Found a solution in meanwhile, which is a bit bulky, but works.
I only edit from remote desktop, but start the python execution from a separate ssh console. This way, kmsdrm driver works.
When the raspi will be installed into a standalone hardware device, it would anyways autostart the python script.
import sys
import pygame
def run_game():
pygame.init()
screen=pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Battle")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I get this error:
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/test/test.py", line 3, in
import pygame
ModuleNotFoundError: No module named 'pygame'
I have installed pygame and did the import command on the cmd on windows.
How can I solve the problem?
If you have more than one python editor in your pc, make sure you installed pygame into the right python editor
This kind of problem arises because of two resiongs:
If you have more then on python version in your system and you don't set the python environment yet for default python version to use.
Some time editor not response well after installing packages or library. In this case just close the editor and restart it. Most of the time it helps to fetch the library.
Found out about Curses a couple days ago and wanted to start messing around with it to see what I could create.
I followed some instructions for installation on another thread and it installed just fine. However, when I attempt to run initscr() (which initializes the screen, it crashes)
Here's the Error text:
{Traceback (most recent call last):
File "C:\Users\ADimi\Desktop\Wing Workspace\test.py", line 2, in <module>
stdscr = curses.initscr()
File "C:\Users\ADimi\AppData\Local\Programs\Python\Python37\lib\curses\__init__.py", line 30, in initscr
fd=_sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'}
To recreate:
You can run this in either, idle or a seperate file. For me this directly causes the error, specficially line 2. stdscr = curses.initscr()
import curses
stdscr = curses.initscr()
From what I'm understanding from the error itself, _sys.__stdout__ is returning a NoneType and thus leading to a crash.
I tried to find similar issues online and have had no luck. Any help at this point could help out a lot I'd love to get this working.
Here's a link about curses:
https://docs.python.org/2/howto/curses.html#curses-howto
Here's where I found out about curses, the first comment is the installation instructions I followed:
What is needed for curses in Python 3.4 on Windows7?
Thanks in advance.
EDIT:
I have some what found a fix. There is a windows tailored version of curses found here: https://pypi.org/project/windows-curses/#files
After uninstalling the old curses and installing this you must follow some intermediate steps:
Create a file with this test code:
import curses
import time
screen = curses.initscr()
screen.clear()
time.sleep(4)
Create a path for 'python' for your cmd prompt:
https://www.pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/
Now using the cmd prompt open the file:
python test.py
You should be presented with a black screen for 4 seconds, then it will close.
This is the only work around I've come across, I will keep searching and updating as I go thank you.
Ok I know this has been asked/answered a bunch of times, but the solutions are not working in my situation. I created a Python script using 2.7 on a headless linux terminal on a Raspberry Pi, it works perfectly in that environment.
The script contains an sqlite3 database. On my Windows 10 machine, if I simply double click the Python script from Desktop it creates the database.db file, but the Python command prompt closes before I can see the output.
Running the Python script from the Windows Command prompt eg 'start C:\Users\User\Desktop\script.py' does exactly the same thing
Using Run and 'cmd /k C:\Users\User\Desktop\script.py' does make the Command window persist, but then it errors on the line opening the sqlite3 database:
Traceback (most recent call last):
File "C:\Users\User\Desktop\script.py", line 91, in <module>
conn = sqlite3.connect("database.db")
sqlite3.OperationalError: unable to open database file
The reason Im on Windows at all is Im trying to build a GUI with Tkinter and my R Pi has no GUI, help appreciated
You need to pass the full path like C:\Users\User\Desktop\database.db.
The reason is when running from 'cmd /k C:\Users\User\Desktop\script.py' then the working directory where the command is called is different and it tries to look database.db in that folder, and gives you the error.
You can check what the present dir is by :
import os
print os.getcwd()
I am trying to run pygame on the PyCharm IDE, I have installed the latest version of pygame for python 3.5 and have added it to the project interpreter. I installed pygame from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame and copied it too python35-32/Scripts/. The test program below runs fine in the python shell but I want to run it from PyCharm.
I have been trying to solve this for the past hour and I've hit a wall.
When I run this simple test program to see if pygame is working:
import pygame
pygame.init()
pygame.display.set_mode((800, 800))
I get this error:
Traceback (most recent call last):
File "C:/Users/jerem/PycharmProjects/Games/hello_pygame.py", line 1, in <module>
import pygame
File "C:\Users\jerem\AppData\Roaming\Python\Python35\site-packages\pygame\__init__.py", line 141, in <module>
from pygame.base import *
ImportError: No module named 'pygame.base'
Any help would be greatly appreiciated!
Thanks again,
JC
Follow the instructions provided here. I think its related to the problem you are having on pygame and PyCharm on windows.
How do I download Pygame for Python 3.5.1?
its bcus pycharme has not recognised you're env or working on wrong env
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
check this