Creating a distributable EXE

Installing PyInstaller

From console:

From Pycharms (Windows) - part 1

From Pycharms (Windows) - part 2

Modify your code

The main difficulty with the manner that pyInstaller works, is the way it handles “extra” files you want to use (such as images, music/sounds etc). When run, the EXE file will put these in a randomly created temporary folder. You need to modify your code so it knows where to find these files. Add the following function to your code:

def get_true_filename(filename):
    try:
        # Hack for pyInstaller. Refer https://stackoverflow.com/a/13790741
        base = sys._MEIPASS
    except Exception:
        base = os.path.abspath(".")
    return os.path.join(base, filename)

Now, where ever you are loading those images, sounds etc in your project, modify the code as follows:

From:

SPRITE = pygame.image.load("my_sprite.png")

To:

SPRITE = pygame.image.load(get_true_filename("my_sprite.png"))

Create the EXE file

Using pyCharm:

Using console:

pyinstaller --clean -–onefile myscript.py --add-data *.wav;. --add-data *.png;. --add-data *.jpg;.

Notes