Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coupling up to PyInstaller to create a standalone executable for distribution #59

Open
lordmauve opened this issue Oct 30, 2017 · 2 comments
Labels
enhancement New feature or request minor

Comments

@lordmauve
Copy link
Owner

Originally reported by: Steven Pool (Bitbucket: steven-pool, GitHub: Unknown)


Creating a tool that will generate a portable executable so that people can easily share their code with friends etc [Using PyInstaller]


@lordmauve
Copy link
Owner Author

Original comment by Steven Pool (Bitbucket: steven-pool, GitHub: Unknown):


Attempted to have a look at this during PyCon. Here are [lots of] things I found out:

PyInstaller options that are useful:

--add-data srcPath:dstPath this will copy the files/folders from src to a specified destination in the compiled structure. This will be good to include images/sound files to get all that data across. There is also a similar --add-binary for binary files

--hidden-import to specify a library if it is imported in a non standard way and you want to make sure it's included

You can generate the .spec file programatically and generate the output by simply calling pyinstaller

Please make sure pgzero1.2 or higher is used (Fix for issue #23) as this makes the whole process a little easier

PyInstaller Issues

PyInstaller does not pull in data files with imports that it finds. I hope Daniel can comment on the bug he found, because I can not find it.

This means that when PyInstaller pulls in pygame it misses some key files ( Mainly the non python files in the pygame package: python/site-packages/pygame/[freesansbold.ttf, pygame_icon.bmp ] )

Tracing this issue can be found in the pkg_resources package - Mainly without PyInstaller, PyGame can find the files through the DefaultProvider class. With PyInstaller it fails to locate the resources.

To see the files that were issues, you can look at the pygame hook for pkg_resources (pygame/pkgdata.py) and print the pkgname/identifier in the hooked function.

Hacky fix

Until we understand why the bugs in PyGame are there, we have a hacky fix! This is a different way I found than in PyCon that I think is slightly better:

(1) Change pgzero's loader.py to not pass in None to pygame.Font, which is an issue specific to PyInstaller, but instead the default font of pygame

#!python
FALLBACK_FONT = 'freesansbold.ttf'
...

    if fontname is None:
        font = ptext._font_cache.get(key)
        if font:
            return font
        font = pygame.font.Font(FALLBACK_FONT, fontsize)

(2) Checks if the file currently imports pgzrun, if not then import it and set it up (And pgzrun.go() at the end of the script)

(3) Calls PyInstaller on the file (Along with adding the sound/images/etc directories and copying the pygame's default font in site-packages/pygame to the output folder - still using the --add-binary flag)

(4) Add some form of bundling ( The -F flag in PyInstaller seems to have issues, although I haven't had chance to check it out so it could work out fine)

Putting all this information here before I forget....

@lordmauve lordmauve added minor enhancement New feature or request labels Feb 25, 2018
@AnthonyBriggs
Copy link

I managed to create a standalone executable and documented the process here - it might be helpful. Most things, including the loader, 'just worked' once I'd figured out what was going on, though there was a bit of head scratching to get there.

There are options in Pyinstaller to explicitly pull in data files and folders, so the Pygame font and bmp issues are probably not insurmountable - though I did end up just commenting out the default icon to keep things rolling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request minor
Projects
None yet
Development

No branches or pull requests

2 participants