Pyinstaller with pygame

I searched a lot in the PyInstaller doc to get my own game working. I don’t know much about Ubuntu, but I got everything working in Windows and it should be very similar. The key here is to get PyInstaller to package your ressources (images, sounds, etc.) with your Python code. The best distribution is to use the pyinstaller --onefile main.py console command in your directory to get a single executable. PyInstaller will make a dist folder and a main.spec file. The .spec file contains all the options PyInstaller uses for making your executable. For a --onefile executable, it should look like this:

# -*- mode: python -*-

block_cipher = None

a = Analysis(['..\\src\\bitmessagemain.py'],
         pathex=['C:\\example\\pyinstaller\\bitmessagemain'],
         binaries=None,
         datas=None,
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)

pyz = PYZ(a.pure, a.zipped_data,
         cipher=block_cipher)
exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      a.binaries, 'BINARY')],
      name="bitmessagemain",
      debug=False,
      strip=None,
      upx=True,
      console=False , icon='src\\images\\can-icon.ico')

Be sure to run PyInstaller once to have it create the .spec file, even if the resulting executable doesn’t work.

a is an object that collects stuff to put in your executable. You want to add your ressource folders in the datas= part. To do this, create a list:

added_files = [
     ( 'data', 'data' ),
     ( 'sfx/*.mp3', 'sfx' ),
     ( 'src/README.txt', '.' )
     ]

The first line in this example adds all the content of the folder data located in the same folder as you main.spec and main.py and adds it to the data folder inside the executable. You want to keep your paths working, so the two entries of the tuple should usually be the same. The second line adds all .mp3 files from sfx to the sfx folder inside the exectuable, and so on. Add all your ressources in this list, and be careful to keep your paths valid.

You can then modify your .spec (which is standard python code):

# -*- mode: python -*-

block_cipher = None

    added_files = [     #Any variable name works
     ( 'data', 'data' ),
     ( 'sfx/*.mp3', 'sfx' ),
     ( 'src/README.txt', '.' )
     ]

a = Analysis(['..\\src\\bitmessagemain.py'],
         pathex=['C:\\example\\pyinstaller\\bitmessagemain'],
         binaries=None,
         datas=added_files,    #Dont forget to change this line!
         hiddenimports=[],
         hookspath=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)
...

The last very important step is to add those two lines of code at the start of your main.py:

if getattr(sys, 'frozen', False):
    os.chdir(sys._MEIPASS)

The trick here is that when you run your executable, PyInstaller will unpack all your data files in a hidden _MEIPASS folder. You want to set your current directory to this folder so that your code finds your ressources and your paths stays valid.

Finally, I would advise you to use the os.path.join function for all your paths in your code. It’ll make the paths portable to other platforms.

On a side note, if you use the –onedir option, do the same thing but add the two lines:

if getattr(sys, 'frozen', False):
    os.chdir(os.path.dirname(sys.executable))

If you do this and your executable closes immediately when double clicking (in Windows), you need to change the console=False to console=True line and then type cmd /k COMPLETEPATHTOYOUREXECUTABLE in the search field in the StartMenu. This will run your .exe and keep the console open so you can see the error log. It will probably be asking for some file you forgot to include, that way you can add it to the .spec, rebuild your .exe with pyinstaller main.spec and try again.

Hope this helps others too, the info on the web is quite obscure for PyInstaller+Pygame.

Leave a Comment