Packaging a Pygame Game as a Windows Exe: a Lesson in Fail

For my first #1GAM game Yet Another Space Shooter I didn’t bother figuring out how to make the game easily installable and playable. I was focussed on just getting my first game over the finish line. As a result only a handful of people have played it and one of them was my brother.

Here in month two it is time to make things easy for anyone wanting to play my games. To that end it’s time to figure out how to distribute my python/pygame games as a Windows executable.

First of all, why do I have to do this? Well, without a windows exe someone wanting to play the game currently has to retrieve the game from github (no simple task for anyone unfamiliar with Git), install Python and Pygame if they don’t already have them, then run “python”. It’s not super complicated but if someone asked me to do all this before I could play their game I probably wouldn’t do it. With a Windows exe anyone running Windows can simply download the exe and run it.

Along the way to Windows executable nirvana I ran into some problems which I thought I’d document. Partly for the education and amusement of others. Partly so future Andrew can read this when he forgets everything he’s learned.

Step 1, check that I can manually run the game under Windows. I use Ubuntu ie Linux as my primary operating system. Before I even get to packing my game into an executable I need to check it works in Windows at all.

I rebooted into Windows, downloaded and installed both Python and Pygame. Then I tried to run my game…

Learning Opportunity 1: Be aware of Python versions

The Python world is divided into 2.x and 3.x and never the twain shall meet. I didn’t realize it but Ubuntu ships with 2.x pre-installed. I just started using it without even checking what version I had. When I had to manually install Python in Windows I saw 2.7 and 3.3, thought “newer is better right?” and installed 3.3.

When I ran my program I got a syntax error. That’s weird. I rebooted into Linux, ran it there and it ran fine. Some Googling later I discovered that this was because I had written the code using 2.7 but was running 3.3 in Windows.

Uninstall Pygame (just in case), uninstall Python 3.3, download and install Python 2.7, reinstall Pygame.

Around about here I realized that both notepad and wordpad suck for reading code. Notepad++ seems like a viable alternative.

Learning Opportunity 2: Be aware of 32 Vs 64 bit Python

Running my game again I got a weird error about a DLL failing to load and something not being a valid executable. It was coming from within Pygame itself.

Some more googling later I discovered something else. Pygame is 32 bit and does not play nice with 64 bit Python. I have a 64 bit laptop so I automatically grabbed the 64 bit version of Python. More bits is better right?

Uninstall Pygame (just in case), uninstall Python 2.7 64 bit, download and install Python 2.7 32 bit, reinstall Pygame.

Huzzah! I can now manually run my game under Windows.

Step 2, bundle it all up as an exe.

Once I could actually run my Python game the rest was relatively easy. Documentation for the various Python packing systems is pretty sparse but my needs are not complicated.

I spent several hours tinkering with py2exe, PyInstaller and cx_freeze. They were all fine to spit out an exe that didn’t need any files. As soon as you needed files things got a little more interesting. In the end I focussed my attention on cx_freeze.

Rather than telling you what’s involved I’ll just show you. It’s pretty self explanatory.

Cx Freeze requires a little script that you can then rerun whenever you want to regenerate your exe. You can find my first cx freeze script in my github account.

I now possess the magic of exe generation!