How to add a TrueType font file to a pyinstaller executable for use with Pygame?

Viewed 2096

I have created a python file named breakout.py and want to create an exe with pyinstaller. The python file requires the file ARCADECLASSIC.TTF in the same directory as the python script in order to run. Also I want to be able to run the exe file without having to have the font file aswell. I have tried multiple pyinstaller commands to try to do this.

I tried this command pyinstaller --add-binary "ARCADECLASSIC.TTF;." --onefile breakout.py and recieved a permission denied error number 13.

Then I tryed editing my code so that the font file would be inside a directory called fonts and tried this command pyinstaller --add-binary "fonts/ARCADECLASSIC.TTF;fonts" --onefile breakout.py which ran with no errors but when I ran the exe file an error came saying there is no file named fonts/ARCADECLASSIC.TTF so crashed.

I have now also tried specifying the full directory of the font file and it still doesn't work.

In case this helps my OS is Windows and I am using python 3.8.5 64 bit with pyinstaller 4.0.

Firstly is what I am trying to do possible and secondly if it is possible how do I do it and what am I doing wrong.

1 Answers

You will need to create a .spec file and mention what you will want to include in it like font or other images.

# -*- mode: python -*-

block_cipher = None


a = Analysis(['your python file.py'],
             pathex=['C:\\path\\to\\directory'], # just the directory not the file
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

a.datas += [('ttf file','path\\to\\ttf\\file', "DATA")]

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

exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      name='Your App Name',
      debug=False,
      strip=False,
      upx=True,
      console=False # set True if command prompt window needed
)

in your actual python file where the game is include this.

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

whenever you want to load your font do it like this.

font = pygame.font.Font(resource_path("Font.ttf"), size)

after you have that in the your main python file, in your terminal type in this with the spec file you created.

pyinstaller yourspecfile.spec

this will create your executable file and it should work by itself.

this is what my spec file looks like from my previous project # -- mode: python --

block_cipher = None


a = Analysis(['lol.py'],
         pathex=['C:\\games\\snake'],
         binaries=[],
         datas=[],
         hiddenimports=[],
         hookspath=[],
         runtime_hooks=[],
         excludes=[],
         win_no_prefer_redirects=False,
         win_private_assemblies=False,
         cipher=block_cipher)

a.datas += [('8min.ttf','C:\\games\\snake\\8min.ttf', "DATA")]

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

exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      name='Snake',
      debug=False,
      strip=False,
      upx=True,
      console=False)
Related