Skip to content

Incomplete data file instructions in PyInstaller tutorial #773

@SirGnip

Description

@SirGnip

Documentation request:

The PyInstaller bundling tutorial isn't handling paths correctly for user-provided data files: https://arcade.academy/tutorials/bundling_with_pyinstaller/index.html#handling-data-files

The instructions work when doing a one-directory bundle, but not when doing a one-file executable (--onefile). The most direct way to fix this is to add a little bit of code at the beginning of your app that changes the working directory if the app is running from a PyInstaller bundle. This way, games can use relative path names for resources regardless of whether the code is running normally, or running inside a bundle. Not the most sophisticated or flexible of approaches, but it probably most direct and understandable for beginners.

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
    print("Game is running from a PyInstaller bundle (aka 'frozen').")
    print("Therefore, changing current working directory to the root of bundle directory:", sys._MEIPASS)
    os.chdir(sys._MEIPASS)

The myriad of ways you can get at different directories related to running in a bundle are described here: https://pyinstaller.readthedocs.io/en/stable/runtime-information.html (which I'll link to in the PyInstaller tutorial).

I'll have a PR up for this, hopefully soon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions