Search code examples
pygamegoogle-colaboratory

How to create a video device for google colab?


I am trying to create a simple game using pygame on google collab, but on executing the code pygame.display.set_mode() I get the error No available video device. So how do I resolve it?


Solution

  • You can suppress pygame's attempt to use a real display device by telling SDL to use a dummy driver like:

    !pip install pygame 
    
    import os
    os.environ['SDL_VIDEODRIVER']='dummy'
    import pygame
    pygame.display.set_mode((640,480))
    

    which for me emits:

    Collecting pygame
      Downloading https://files.pythonhosted.org/packages/b3/5e/fb7c85304ad1fd52008fd25fce97a7f59e6147ae97378afc86cf0f5d9146/pygame-1.9.4-cp36-cp36m-manylinux1_x86_64.whl (12.1MB)
        100% |████████████████████████████████| 12.1MB 1.9MB/s 
    Installing collected packages: pygame
    Successfully installed pygame-1.9.4
    pygame 1.9.4
    Hello from the pygame community. https://www.pygame.org/contribute.html
    <Surface(640x480x8 SW)>
    

    (obvs., this won't use the GPU for the dummy "display"; I'm assuming you only want to use the colab GPU for AI/ML)