pygame.error: video system not initialized [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# pygame.error: video system not initialized [Solved]

The "pygame.error: video system not initialized" most commonly occurs when you forget to call pygame.init() to initialize all imported PyGame modules or call pygame.quit() incorrectly.

To solve the error make sure your PyGame application is initialized and uninitialized correctly.

The first thing you should verify is that you've called the pygame.init() method at the top of your file.

Here is an example of a basic setup of a PyGame application from the docs.

main.py
import pygame # ๐Ÿ‘‡๏ธ Call pygame.init() here pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() running = True while running: # pygame.QUIT event runs when the user clicks X to close the window for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Fill the screen with a color screen.fill("purple") # YOUR GAME HERE # flip() the display to put your work on screen pygame.display.flip() clock.tick(60) # ๐Ÿ‘‡๏ธ Uninitialize all `pygame` modules pygame.quit()

Notice that we called the pygame.init() method at the top.

main.py
pygame.init() screen = pygame.display.set_mode((1280, 720)) clock = pygame.time.Clock() running = True

The method initializes all imported PyGame modules.

You can also initialize modules manually, however, calling pygame.init() is more convenient as it initializes all of your imported PyGame modules automatically.

Calling pygame.init() multiple times won't cause any issues as subsequent calls have no effect.

If you try to use a module before calling pygame.init(), the error is raised because the modules have not yet been initialized.

Therefore, make sure to call pygame.init() at the top as shown in the code sample.

Make sure to also call the pygame.display.set_mode() method after calling pygame.init().

main.py
pygame.init() screen = pygame.display.set_mode((1280, 720))

The pygame.display.set_mode() method creates a display surface of the specified size.

# Make sure to call pygame.quit() at the end, not inside a loop

Another common cause of the error is calling pygame.quit() before your code is done running, e.g. in a while loop.

The pygame.quit() method uninitializes all PyGame modules that have previously been initialized.

In other words, pygame.quit() reverses your call to the pygame.init() method.

When the Python interpreter shuts down, the pygame.quit() method is called automatically, however, you can also call the method in your code to terminate the PyGame resources and continue.

  1. Make sure the pygame.quit() method is not called multiple times (e.g. in a while loop).
  2. Make sure the method is called after your application loop.
main.py
while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill("purple") pygame.display.flip() clock.tick(60) # โœ… Call pygame.quit() here pygame.quit()

Notice that the method should be called after the application loop.

You can also try to call the exit() function after calling pygame.quit() if the error persists.

main.py
while running: # ... pass pygame.quit() exit()

You can also call the exit method on the sys module.

main.py
import sys while running: # ... pass pygame.quit() sys.exit()

The pygame.quit() method uninitializes all PyGame modules but it doesn't exit your program.

The sys.exit() method is used to exit the program.

# Using PyGame without opening a visible display

If you use PyGame without opening a visible display (e.g. for testing), try setting the SDL_VIDEODRIVER environment variable.

main.py
import os # ๐Ÿ‘‡๏ธ Set SDL_VIDEODRIVER environment variable os.environ['SDL_VIDEODRIVER'] = 'dummy' import pygame pygame.init() pygame.display.set_mode((1,1)) while 1: events = pygame.event.get() for e in events: pass

Setting the SDL_VIDEODRIVER environment variable to dummy makes it so the dummy video driver is used.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev