17

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.

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • 3
    Remember that pygame is built on top of the C library SDL, so you might be able to find some additional steps to try by googling for the error message "No available video device" as it relates to SDL. – Mark Hildreth Apr 10 '13 at 18:26
  • 2
    Can we see some of the code in pygameHelloWorld.py prior to where you set the display mode? Are you calling init on pygame first? – Haz Apr 10 '13 at 18:44
  • Also, the documentation for pygame.display recommends not passing in a depth value to the set_mode function. I don't think it would be causing your problem, but have you tried just calling pygame.display.set_mode((500,400))? – Haz Apr 10 '13 at 18:49
  • @Haz [pygameHelloWorld.py](http://inventwithpython.com/pygameHelloWorld.py), this script is from book *Invent you own computer games with Python*. So I was doing the examples. – Santosh Kumar Apr 11 '13 at 04:51
  • Hmmm... the code from here: http://inventwithpython.com/chapter17.html (which I'm assuming is what you're working with) looks correct. What version of Python are you using? – Haz Apr 11 '13 at 14:31
  • @Haz I've mentioned in the question. – Santosh Kumar Apr 11 '13 at 14:39
  • 1
    Like Mark suggested, I think this is probably an issue with SDL rather than with Pygame. Have you tried running any other applications that depend on libsdl? – Haz Apr 11 '13 at 17:56
  • What does `echo $DISPLAY` return? – Glitch Desire Apr 24 '13 at 22:06
  • @Oshawott It returns `:0`. – Santosh Kumar Apr 25 '13 at 04:06
  • You say you have all the dependencies installed from that page you link, but I don't see SDL on that page. Is SDL installed? – David Jay Brady Apr 15 '16 at 20:35

8 Answers8

28

If you are running Pygame on a UNIX system, like a Linux server, try using a DummyVideoDriver:

import os
os.environ["SDL_VIDEODRIVER"] = "dummy"
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
fushan
  • 419
  • 4
  • 5
  • 3
    I no longer get the error, but the program is stuck. This is all I get in the terminal: `pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html ` – Paul Apr 03 '20 at 16:38
  • 1
    This does not solve the problem, setting to "DUMMY". This routes around an underlying issue – Derek_P Mar 04 '22 at 19:37
6

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:

iKlsR
  • 2,642
  • 6
  • 27
  • 45
  • 4
    1. I get `pygame.error: video system not initialized`. 2. And I get `sdl.c:1:17: fatal error: SDL.h: No such file or directory` when compiling. The second one is not the one I expected because I installed SDL\* by package manager, so no chance of misconfiguration. – Santosh Kumar Apr 12 '13 at 06:06
  • @SantoshKumar thats a simple error, is the include directory in your path? – iKlsR Apr 12 '13 at 15:22
  • I added but nothing happened. – Santosh Kumar Apr 12 '13 at 17:30
  • 1
    When you installed SDL by package manager, did you install v1.2 or v2.x? – Haz Apr 18 '13 at 14:00
  • The video system not being initialized is just because you never called pygame.display.init(). If your problem is like mine, however, which it probably isn't because I am doing this in an Ubuntu virtual box, you will actually get a non-empty list from this command. – trevorKirkby Jun 04 '14 at 18:46
  • @someone-or-other `pygame.init()` should do this automatically similar to SDL's `SDL_Init(SDL_INIT_EVERYTHING)`. – iKlsR Jun 04 '14 at 19:12
  • It worked after I called pygame.display.init() though, so maybe try it. – trevorKirkby Jun 04 '14 at 20:16
2

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.

Remolten
  • 2,614
  • 2
  • 25
  • 29
  • Do you have the proper GPU drivers installed? If you're running a desktop you need to install the drivers that came with on its CD not just the ones Windows suggest. That's all I can suggest. – Remolten Apr 24 '13 at 12:04
  • Its an *oooold* CRT monitor, and I don't think any driver came with this monitor. – Santosh Kumar Apr 25 '13 at 04:05
0

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.

Tom
  • 2,369
  • 13
  • 21
0

Try the following:

import pygame
from pygame.locals import *
pygame.init()
Armster
  • 772
  • 1
  • 9
  • 25
0

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

Francesco
  • 523
  • 4
  • 25
  • 4
    Please don't just post some tool or library as an answer. At least demonstrate [how it solves the problem](https://meta.stackoverflow.com/a/251605) in the answer itself. – Yunnosch Oct 02 '20 at 08:09
  • Added details to my previous answer – Francesco Oct 02 '20 at 14:04
0

I solved it with the three commands below:

sudo apt-get update

sudo apt-get dist-upgrade

sudo apt-get install libsdl1.2debian:i386
June7
  • 19,874
  • 8
  • 24
  • 34
freeman
  • 1
  • 1
0

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"
Marthinwurer
  • 116
  • 1
  • 6