How can I resolve the "pygame.error: No available video device" error in pygame? [duplicate] - pygame

I have this setup:
$ python -V
Python 2.7.2+
$ python -c "import pygame; print pygame.__version__"
1.9.1release
When I run a pygame script, I get this error:
Traceback (most recent call last):
File "/home/santosh/tmp/pygameHelloWorld.py", line 8, in <module>
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.error: No available video device
I am using Ubuntu and have install pygame with apt-get. Additionally I have install all dependencies mentioned on this pygame wiki page.

If you are running Pygame on a UNIX system, like a Linux server, try using a DummyVideoDriver:
import os
os.environ["SDL_VIDEODRIVER"] = "dummy"

From similar experience the most likely problem is something is wrong with one of your SDL packages. Try running the following.
import pygame
pygame.init()
pygame.display.list_modes()
If you get back an empty list, it's definitely because of the packages. Try reinstalling them.
Possibly related:
SDL init failure, reason is: No available video device
SDL init failure, reason is: No available video device in ubuntu 12.04 LTS

This problem might only affect Windows XP. Try adding one of these to your code:
import os
os.environ['SDL_VIDEODRIVER']='windib'
or
import os
os.environ['SDL_VIDEODRIVER']='windlib'
Good luck.

Wild guess - is the DISPLAY environment variable set and/or required by the library, and not available? This could be the case if you're ssh-ing into the Linux machine you're running this on without providing the -X option.

Try the following:
import pygame
from pygame.locals import *
pygame.init()

On Linux you can solve the problem using x11.
Verify that your SDL was built with x11 support, otherwise build it yourself only after that you added some libraries in this way:
sudo apt install xorg-dev libx11-dev libgl1-mesa-glx
Maybe xorg-dev already installs libx11-dev
Build SDL from source and enjoy

I solved it with the three commands below:
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get install libsdl1.2debian:i386

I was getting this error while trying to render an OpenAI gym in a jupyter notebook. Following fushan's answer got it to stop erroring, but it only rendered a black image. Looking at https://wiki.libsdl.org/FAQUsingSDL, I figured out that I could use directfb as the renderer. The code ended up being:
import os
os.environ["SDL_VIDEODRIVER"] = "directfb"

Related

I'm having issues getting pygame to work as I don't understand how to assign path [duplicate]

I have installed python 3.3.2 and pygame 1.9.2a0. Whenever I try to import pygame by typing:
import pygame
I get following error message :
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pygame
ImportError: No module named 'pygame'
>>>
I went through some of the questions related to this error but none of the solution helped.
I have 64 bit machine with Win7 OS
go to python/scripts folder, open a command window to this path, type the
following:
C:\python34\scripts> python -m pip install pygame
To test it, open python IDE and type
import pygame
print (pygame.ver)
It worked for me...
Here are instructions for users with the newer Python 3.5 (Google brought me here, I suspect other 3.5 users might end up here as well):
I just successfully installed Pygame 1.9.2a0-cp35 on Windows and it runs with Python 3.5.1.
Install Python, and remember the install location
Go here and download pygame-1.9.2a0-cp35-none-win32.whl
Move the downloaded .whl file to your python35/Scripts directory
Open a command prompt in the Scripts directory (Shift-Right click in the directory > Open a command window here)
Enter the command:
pip3 install pygame-1.9.2a0-cp35-none-win32.whl
If you get an error in the last step, try:
python -m pip install pygame-1.9.2a0-cp35-none-win32.whl
And that should do it. Tested as working on Windows 10 64bit.
I was trying to figure this out for at least an hour. And you're right the problem is that the installation files are all for 32 bit.
Luckily I found a link to the 64 pygame download! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
Just pick the corresponding version according to your python version and it should work like magic. The installation feature will bring you to a bright-blue screen as the installation (at this point you know that the installation is correct for you.
Then go into the Python IDLE and type "import pygame" and you should not get any more errors.
Props go to #yuvi who shared the link with StackOverflow.
open the folder where your python is installed
open scripts folder
type cmd in the address bar. It opens a command prompt window in that location
type pip install pygame and press enter
it should download and install pygame module
now run your code. It works fine :-)
I had the same problem and discovered that Pygame doesn't work for Python3 at least on the Mac OS, but I also have Tython2 installed in my computer as you probably do too, so when I use Pygame, I switch the path so that it uses python2 instead of python3. I use Sublime Text as my text editor so I just go to
Tools > Build Systems > New Build System and enter the following:
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
}
instead of
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
in my case. And when I'm not using pygame, I simply change the path back so that I can use Python3.
The current PyGame release, 1.9.6 doesn't support Python 3.9. I fyou don't want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):
pip install pygame==2.0.0.dev20
or try to install a pre-release version by enabling the --pre option:
pip install pygame --pre
Resolved !
Here is an example
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
try this in your command prompt:
python -m pip install pygame
I was getting the same error. It is because your version of Pygame is not compatible with your version of Python or Pydev. Go to this link and get the proper version of Pygame for your current version of Python. Ctrl F to find it faster or click on the word python in blue. up at the top. While you instal Pygame it should find the Python path by itself. At least mind did any ways. I run Pygame through Eclipse with Python 3.4.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Since no answer stated this:
Make sure that, if you are using a virtual environment, you have activated it before trying to run the program.
If you don't really know if you are using a virtual environment or not, check with the other contributors of the project. Or maybe try to find a file with the name activate like this: find . -name activate.
Install and download pygame .whl file.
Move .whl file to your python35/Scripts
Go to cmd
Change directory to python scripts
Type:
pip install pygame
Here is an example:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
I just encountered the same problem and found that I am having multiple interpreters of the different versions installed in my system and pygame got installed in one of them when I installed it using command but in my IDE another interpreter was selected so this messed up my system, try to see if you are also having the same situation.
Just use this command in the terminal python3 -m pip install -U pygame --user
I am a quite newbie to python and I was having same issue. (windows x64 os)
I have solved, doing below steps
I removed python (x64 version) and pygame
I have downloaded and installed python 2.6.6 x86: https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
I have downloaded and installed pygame (when installing, I have chosen the directory that I installed python): http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
Works well :)
You don't need 64 bit Python on Win64 system, just install the 32bit versions of both Python and Pygame and they will work just fine (and there is a ton more modules for them anyways).
I’m using the PyCharm IDE. I could get Pygame to work with IDLE but not with PyCharm. This video helped me install Pygame through PyCharm.
https://youtu.be/HJ9bTO5yYw0
(It seems that PyCharm only recognizes a package; if you use its GUI.)
However, there were a few slight differences for me; because I’m using Windows instead of a Mac.
My “preferences” menu is found in: File->Settings…
Then, in the next screen, I expanded my project menu, and clicked Project Interpreter. Then I clicked the green plus icon to the right to get to the Available Packages screen.
I ran into the error a few days ago! Thankfully, I found the answer.
You see, the problem is that pygame comes in a .whl (wheel) file/package. So, as a result, you have to pip install it.
Pip installing is a very tricky process, so please be careful. The steps are:-
Step1. Go to C:/Python (whatever version you are using)/Scripts. Scroll down. If you see a file named pip.exe, then that means that you are in the right folder. Copy the path.
Step2. In your computer, search for Environment Variables. You should see an option labeled 'Edit the System Environment Variables'. Click on it.
Step3. There, you should see a dialogue box appear. Click 'Environment Variables'. Click on 'Path'. Then, click 'New'. Paste the path that you copies earlier.
Step4. Click 'Ok'.
Step5. Shift + Right Click wherever your pygame is installed. Select 'Open Command Window Here' from the dropdown menu. Type in 'pip install py' then click tab and the full file name should fill in. Then, press Enter, and you're ready to go! Now you shouldn't get the error again!!!
First execute python3 then type the command import pygame,now you can see the output
For this you have to install pygame package from the cmd (on Windows) or from terminal (on mac). Just type pip install pygame
.If it doesn't work for you, then try using this statement pip3 install pygame .
If it is still showing an error then you don't have pip installed on your device and try installing pip first.
make sure if you are on windows that your library directory is added to path
This may happen when pygame didn't installed, install the pygame first
pip
pip install pygame
if dont work update the PIP by goto python install folder and type
python -m pip install --upgrade pip
hope it work
Try this solution:
Type in to cmd (Windows):
C:\Users\'Your name'> pip install -U pygame
You should remove python -m, py -m, python3 -m before the pip
Also remove --user behind it.
It will said:
C:\Users\viait>pip install -U pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
Downloading pygame-2.1.2-cp310-cp310-win_amd64.whl (8.4 MB)
---------------------------------------- 8.4/8.4 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2
Then test it in your IDE or cmd:
(CMD example)
C:\Users\viait>python
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.1.2 (SDL 2.0.18, Python 3.10.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
(IDE example)
import pygame
You can do this without any errors.
You could use
pip install pygame
but if you use IDE like PyCharm, then you could just either install it from Python Packages or use right click at the package name then left click on Show Context Actions then left click on Install package pygame
(Personally, I recommended using Python Packages for the package installing because it has documentation with it)
You gotta use Pycharm and install it in Terminal using pip install pygame and also after that enter Pycharm and hover on pygame in the "Import pygame" and in Pycharm it will tell you to download that and you can easily download it and enjoy your result
I was having the same trouble and I did
pip install pygame
and that worked for me!

Pygame examples not found in Ubuntu 20.04

I want to learn how to code in Pygame (python3.8.5, Ubuntu 20.04). I got pygame installed but it can find the examples. I tried to search for them manually and didn find them either. It seems that the examples were not installed at all.
Here's what I did so far:
The attempt to install pygame with pip failed due to tons of failed dependencies.
python3 -m pip install -U pygame --user
After searching a bit, I found this command, which installed pygame:
sudo apt install python3-pygame
But it fails when I try to run one of the examples:
user:~$ python3 -m pygame.examples.aliens
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
/usr/bin/python3: Error while finding module specification for 'pygame.examples.aliens' (ModuleNotFoundError: No module named 'pygame.examples')
Any ideas what's wrong?
Thanks!
The Ubuntu Package Maintainers have elected to not include the examples in python3-pygame. Tests and docs are also excluded.
You can access the examples from the pygame github repository.

How to deal with 'ModuleNotFoundError: No module named 'pygame' ' when pygame was installed? [duplicate]

I have installed python 3.3.2 and pygame 1.9.2a0. Whenever I try to import pygame by typing:
import pygame
I get following error message :
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import pygame
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pygame
ImportError: No module named 'pygame'
>>>
I went through some of the questions related to this error but none of the solution helped.
I have 64 bit machine with Win7 OS
go to python/scripts folder, open a command window to this path, type the
following:
C:\python34\scripts> python -m pip install pygame
To test it, open python IDE and type
import pygame
print (pygame.ver)
It worked for me...
Here are instructions for users with the newer Python 3.5 (Google brought me here, I suspect other 3.5 users might end up here as well):
I just successfully installed Pygame 1.9.2a0-cp35 on Windows and it runs with Python 3.5.1.
Install Python, and remember the install location
Go here and download pygame-1.9.2a0-cp35-none-win32.whl
Move the downloaded .whl file to your python35/Scripts directory
Open a command prompt in the Scripts directory (Shift-Right click in the directory > Open a command window here)
Enter the command:
pip3 install pygame-1.9.2a0-cp35-none-win32.whl
If you get an error in the last step, try:
python -m pip install pygame-1.9.2a0-cp35-none-win32.whl
And that should do it. Tested as working on Windows 10 64bit.
I was trying to figure this out for at least an hour. And you're right the problem is that the installation files are all for 32 bit.
Luckily I found a link to the 64 pygame download! Here it is: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame
Just pick the corresponding version according to your python version and it should work like magic. The installation feature will bring you to a bright-blue screen as the installation (at this point you know that the installation is correct for you.
Then go into the Python IDLE and type "import pygame" and you should not get any more errors.
Props go to #yuvi who shared the link with StackOverflow.
open the folder where your python is installed
open scripts folder
type cmd in the address bar. It opens a command prompt window in that location
type pip install pygame and press enter
it should download and install pygame module
now run your code. It works fine :-)
I had the same problem and discovered that Pygame doesn't work for Python3 at least on the Mac OS, but I also have Tython2 installed in my computer as you probably do too, so when I use Pygame, I switch the path so that it uses python2 instead of python3. I use Sublime Text as my text editor so I just go to
Tools > Build Systems > New Build System and enter the following:
{
"cmd": ["/usr/local/bin/python", "-u", "$file"],
}
instead of
{
"cmd": ["/usr/local/bin/python3", "-u", "$file"],
}
in my case. And when I'm not using pygame, I simply change the path back so that I can use Python3.
The current PyGame release, 1.9.6 doesn't support Python 3.9. I fyou don't want to wait for PyGame 2.0, you have to use Python 3.8. Alternatively, you can install a developer version by explicitly specifying the version (2.0.0.dev20 is the latest release at the time of writing):
pip install pygame==2.0.0.dev20
or try to install a pre-release version by enabling the --pre option:
pip install pygame --pre
Resolved !
Here is an example
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
try this in your command prompt:
python -m pip install pygame
I was getting the same error. It is because your version of Pygame is not compatible with your version of Python or Pydev. Go to this link and get the proper version of Pygame for your current version of Python. Ctrl F to find it faster or click on the word python in blue. up at the top. While you instal Pygame it should find the Python path by itself. At least mind did any ways. I run Pygame through Eclipse with Python 3.4.
http://www.lfd.uci.edu/~gohlke/pythonlibs/
Since no answer stated this:
Make sure that, if you are using a virtual environment, you have activated it before trying to run the program.
If you don't really know if you are using a virtual environment or not, check with the other contributors of the project. Or maybe try to find a file with the name activate like this: find . -name activate.
Install and download pygame .whl file.
Move .whl file to your python35/Scripts
Go to cmd
Change directory to python scripts
Type:
pip install pygame
Here is an example:
C:\Users\user\AppData\Local\Programs\Python\Python36-32\Scripts>pip install pygame
I just encountered the same problem and found that I am having multiple interpreters of the different versions installed in my system and pygame got installed in one of them when I installed it using command but in my IDE another interpreter was selected so this messed up my system, try to see if you are also having the same situation.
Just use this command in the terminal python3 -m pip install -U pygame --user
I am a quite newbie to python and I was having same issue. (windows x64 os)
I have solved, doing below steps
I removed python (x64 version) and pygame
I have downloaded and installed python 2.6.6 x86: https://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
I have downloaded and installed pygame (when installing, I have chosen the directory that I installed python): http://pygame.org/ftp/pygame-1.9.1.win32-py2.6.msi
Works well :)
You don't need 64 bit Python on Win64 system, just install the 32bit versions of both Python and Pygame and they will work just fine (and there is a ton more modules for them anyways).
I’m using the PyCharm IDE. I could get Pygame to work with IDLE but not with PyCharm. This video helped me install Pygame through PyCharm.
https://youtu.be/HJ9bTO5yYw0
(It seems that PyCharm only recognizes a package; if you use its GUI.)
However, there were a few slight differences for me; because I’m using Windows instead of a Mac.
My “preferences” menu is found in: File->Settings…
Then, in the next screen, I expanded my project menu, and clicked Project Interpreter. Then I clicked the green plus icon to the right to get to the Available Packages screen.
I ran into the error a few days ago! Thankfully, I found the answer.
You see, the problem is that pygame comes in a .whl (wheel) file/package. So, as a result, you have to pip install it.
Pip installing is a very tricky process, so please be careful. The steps are:-
Step1. Go to C:/Python (whatever version you are using)/Scripts. Scroll down. If you see a file named pip.exe, then that means that you are in the right folder. Copy the path.
Step2. In your computer, search for Environment Variables. You should see an option labeled 'Edit the System Environment Variables'. Click on it.
Step3. There, you should see a dialogue box appear. Click 'Environment Variables'. Click on 'Path'. Then, click 'New'. Paste the path that you copies earlier.
Step4. Click 'Ok'.
Step5. Shift + Right Click wherever your pygame is installed. Select 'Open Command Window Here' from the dropdown menu. Type in 'pip install py' then click tab and the full file name should fill in. Then, press Enter, and you're ready to go! Now you shouldn't get the error again!!!
First execute python3 then type the command import pygame,now you can see the output
For this you have to install pygame package from the cmd (on Windows) or from terminal (on mac). Just type pip install pygame
.If it doesn't work for you, then try using this statement pip3 install pygame .
If it is still showing an error then you don't have pip installed on your device and try installing pip first.
make sure if you are on windows that your library directory is added to path
This may happen when pygame didn't installed, install the pygame first
pip
pip install pygame
if dont work update the PIP by goto python install folder and type
python -m pip install --upgrade pip
hope it work
Try this solution:
Type in to cmd (Windows):
C:\Users\'Your name'> pip install -U pygame
You should remove python -m, py -m, python3 -m before the pip
Also remove --user behind it.
It will said:
C:\Users\viait>pip install -U pygame
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
Downloading pygame-2.1.2-cp310-cp310-win_amd64.whl (8.4 MB)
---------------------------------------- 8.4/8.4 MB 1.7 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.1.2
Then test it in your IDE or cmd:
(CMD example)
C:\Users\viait>python
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.1.2 (SDL 2.0.18, Python 3.10.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
(IDE example)
import pygame
You can do this without any errors.
You could use
pip install pygame
but if you use IDE like PyCharm, then you could just either install it from Python Packages or use right click at the package name then left click on Show Context Actions then left click on Install package pygame
(Personally, I recommended using Python Packages for the package installing because it has documentation with it)
You gotta use Pycharm and install it in Terminal using pip install pygame and also after that enter Pycharm and hover on pygame in the "Import pygame" and in Pycharm it will tell you to download that and you can easily download it and enjoy your result
I was having the same trouble and I did
pip install pygame
and that worked for me!

AttributeError: module 'pygame' has no attribute 'init' [duplicate]

I just downloaded Pygame but is running into an error: AttributeError: module 'pygame' has no attribute 'init'. I was looking around on stackoverflow and someone fixed the error by renaming the files (which I don't know how to do). The directory is the following
Python -> Lib -> site-packages ->pygame
The confusing part for me is inside the pygame folder
pygame > Include, Lib
Include > pygame > (many hh files)
Lib > SitePackages > pygame > init (I know that it has the attribute init)
Since there is '3' pygame folder, is this causing the error for my program?
import pygame, sys
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Just rename your file from pygame.py to myTestGame.py for example; because you're importing the same file when you use import pygame !
It looks as though you have "downloaded" a source distribution of pygame, and then copied the content into site-packages. that works fine for most pure-python libraries, but not for ones with c-extensions, like pygame. You are probably best served by using an installer, as pygame is a bit tedious to build from source.
Visit http://www.pygame.org/download.shtml and choose an installer for your OS. note that the first two links are source downloads, and not what you need.
The most current, best way to install pygame is available at: https://www.pygame.org/wiki/GettingStarted
Currently, for windows, it says to install it like this in a cmd window:
py -m pip install pygame --user
Many windows users have difficulty installing Python, getting the path vars right and then adding in additional modules. A convenient solution I find for many is to use a portable version they can put on USB (Or Desktop) and run anywhere, easily.
Download and install this: Portable Python Pack
Install to USB or Desktop
From the installed folder, open PyScripter (or another if you prefer)
Open and run your PyGame or other Python code.
NOTE: This is Python 2.7, but it has PyGame and a range of other modules packed in- check it out before download and try if suited to your needs!
You have to install pygame. If you did please try it with pip. In terminal or command line type pip install pygame and if that does not work do python -m pip install pygame and if that does not work do py -m pip install pygame
i recommend y'all to use this code
import pygame, sys
from pygame.locals import *
screen = pygame.display.set_mode((600,600))
pygame.display.set_caption("Pygame Title")
def close():
sys.exit()
pygame.quit()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
1 first uninstall your IDE
2 reinstall IDE in another folder
3 goto IDE terminal and install Pygame using pip version 19.0.3
4 Then your problem is solved
it's a common mistake which is not still fixed by now if you are running your code on a IDE like , "VSC" or "pycharm" and such a kind. you will get this error.
MAKE SURE that you have installed pygame correctly using pip and try to run the code using Git Bash (by typing python filename.py) or the python's interpreter.
you can also seek info at www.pygame.org
try:
python -m pip uninstall pygame
python -m pip install pygame
This should reinstall pygame.

how to install pygobject for python3 in fedora18

i try to install pygobject for python 3 in fedora 18
i read this doc https://python-gtk-3-tutorial.readthedocs.org/en/latest/install.html and i try to install jhbuild after that when i type $ jhbuild sanitycheck
get this error : bash: jhbuild: command not found...
i try to install with yum , i install "pygobject3-devel" but this not work with python3
in fedora 18 default python is python2.7.3, i install python3.3.0 with yum and test a first demo code :
#!/usr/bin/python
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
after give chmod +x for this python file can run with this command "./test.py"
and if i try to run with python (with out use #! ....) and type this command "$python test.py" that run and work without any problem but when i type this command "$python3 test.py" i get this error :
Traceback (most recent call last):
File "test.py", line 2, in <module>
from gi.repository import Gtk
ImportError: No module named 'gi'
have any idea ?
thanks
Try installing pygobject3 instead of pygobject3-dev (as a matter of fact, it seems that pygobject3 is already installed in a vainilla installation of Fedora).
After installing that package, you should be able to open your interpreter (python2.7) and do:
from gi.repository import Gtk
If you want to use python3 (I didn't understand if you meant that you tried to use python3 or you want to use python3), it seems that the gnome instrospection bindings are not yet available, according to this page that summarizes the state of different packages python3-related that are being on the works.
EDIT: I just tried installing python3-gobject (which should be installed when installing pygobject3, but it doesn't for some reason) and it seems to be working aswell on python3.