Freezing with PyInstaller & Nuitka

Pygame GUI has some support for turning your application into an executable using either PyInstaller or Nuitka. Be aware however that these utilities working successfully is also dependent on the version of python you are using, support in any other libraries you are using (including pygame CE) and your own handling of data files.

As a general principle it is better to be slightly back from the bleeding edge of Python progress if you want to successfully freeze your python scripts into an exe. Both nuitka and PyInstaller are community projects and as such are dependent on the python community to keep pace with core python development. There is usually some amount of lag.

That out of the way, lets look at each of them more closely.

PyInstaller

First things first you will need to have the PyInstaller package from PyPI:

pip install pyinstaller

Once that is installed you need to create a .spec file for your project that will handle gathering up all the data files you are using and any other options you might wish to use. There are a lot of options for spec files and you can read more about them in the pyinstaller documentation.

Here is one I cooked up for the pyinstaller_example script in the pygame_gui_examples repository:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- mode: python -*-

block_cipher = None


a = Analysis(['../pyinstaller_test.py'],
             pathex=[],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

a.datas += Tree('data', prefix='data')

pyz = PYZ(a.pure, a.zipped_data,
          cipher=block_cipher)

# enable this to see verbose output
# options = [ ('v', None, 'OPTION')]
exe = EXE(pyz,
          a.scripts,
          # options,
          exclude_binaries=True,
          name='pyinstaller_test_release',
          debug=False, # set this to True for debug output
          strip=False,
          upx=True,
          console=True ) # set this to False this to remove the console
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='pyinstaller_test_release')

Once you have your spec file, and made sure it includes any custom data you are using in your script (fonts, images, sounds, theme files etc) you are ready to try and compile the executable.

pyinstaller path_to_your_spec_file.spec

Pygame GUI now includes a Pyinstaller 'hook' file which should bring in all the data files used by Pygame GUI itself, however if you are using other packages that have their own data files you may have to manage this process yourself.

Good luck, and make yourself known on the GitHub issues page if you have any problems.

Nuitka

As with PyInstaller, when using Nuitka the first step is to download it:

pip install nuitka

Once you've done that you need to carefully specify the Nuitka command line to include all the data directories for pygame_gui (along with any data directories for any other libraries you are using).

The crucial line is:

--include-plugin-directory=pygame_gui/data

Here is an example command line:

nuitka --onefile --plugin-enable=numpy --plugin-enable=pylint-warnings --include-plugin-directory=pygame_gui/data -o package/YourExeName.exe --output-dir=package

There haven't been as many users of the Pygame GUI asking about Nuitka yet, so I am not as familiar with any other issues that may arise. Please check out the official documentation for more information on Nuitka.

As always let us know any issues you have on the GitHub issues page.