Converting PyGame to an executable

In this PyGame with Python 3 programming tutorial, we cover how we can convert our PyGame into an executable, or an installer, which we can then share with people who do not have Python installed, or pygame... etc. We will do this using cx_Freeze, which is a very similar module to py2exe for those who are familiar.

The reason why you may want to convert your game into an executable is so that you can share it with people who may not have PyGame or Python installed.

In order to use cx_Freeze, you will need to download and install it. The official source is: cx_Freeze website/

Some people have had problems with the official source, so you can use a patched version, found here instead:

Make sure you match the bit version (32 or 64 bit) with your bit version of Python.

Once you have cx_Freeze installed, you're ready to convert your game to an executable or an installer.

First, you will want to make a file with the following contents:

import cx_Freeze

executables = [cx_Freeze.Executable("")]

    name="A bit Racey",
    options={"build_exe": {"packages":["pygame"],
    executables = executables


The above is converting the python script contained under the file name:

From there, we specify the name of the application, then we explicitly notify cx_Freeze that we are using the pygame module, and we explicitly tell it that we have an included file, which is racecar.png.

cx_Freeze can sometimes figure out modules that you're using, but sometimes not. Following with typical Pythonic precepts, explicit is better than implicit, so let's be clear!

Once you've done that, you'll want to navigate to your terminal and make your way to the directory containing your Python game script and the script.

Then, type:

python build.

If you get an error stating that Python is not in your path, then give the full path. For example, on Windows, with Python 3.4, you would do:

C:/Python34/python build

From there, you should get a build directory, within it will be an executable that will run your script.

You can also do something like:

python bdist_msi

If you're on a mac, then you would do:

python bdist_dmg

That will generate a Windows installer.

There are various errors and issues that you might run in to while using cx_Freeze. Check out the video for some of the possible issues. Google can also be a massive help, and some times... just sometimes ... the documents can help a ton!

There exists 2 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:

  • Introduction to PyGame
  • Displaying images with PyGame
  • Moving an image around in PyGame
  • Adding boundaries
  • Displaying text to PyGame screen
  • Drawing objects with PyGame
  • Crashing
  • PyGame Score
  • Drawing Objects and Shapes in PyGame
  • Creating a start menu
  • PyGame Buttons, part 1, drawing the rectangle
  • PyGame Buttons, part 2, making the buttons interactive
  • PyGame Buttons, part 3, adding text to the button
  • PyGame Buttons, part 4, creating a general PyGame button function
  • PyGame Buttons, part 5, running functions on a button click
  • Converting PyGame to an executable
    You are currently here.
  • Adding a pause function to our game and Game Over
  • PyGame Icon
  • Sounds and Music with PyGame