Last updated: Apr 11, 2024
Reading timeยท3 min
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.
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.
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.
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()
.
pygame.init() screen = pygame.display.set_mode((1280, 720))
The pygame.display.set_mode()
method creates a display surface of the
specified size.
pygame.quit()
at the end, not inside a loopAnother 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.
pygame.quit()
method is not called multiple times (e.g. in a
while
loop).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.
while running: # ... pass pygame.quit() exit()
You can also call the exit method on the sys
module.
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.
If you use PyGame without opening a visible display (e.g. for testing), try
setting the SDL_VIDEODRIVER
environment variable.
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.
You can learn more about the related topics by checking out the following tutorials: