Wednesday, 30 December 2020

Compiling gettext locales with PyInstaller in Python 3.x

I was compiling a gettext localized (English and French, but probably more in the future) Python script with pyinstaller --onefile palc.py and it works perfectly — except when I try to run it it attempts to use the locales stored in the locales directory (meaning it can't find them if I don't distribute the package with the locales directory). As you can imagine, this is a major drawback and pretty much ruins the point of PyInstaller — in order to distribute it, I have to give a directory along with the package in order for it to work — though, as I'm going to show you, it doesn't work even with that.

Here is the main question:

Is it possible (preferably not too difficult or something that would require heavy rewriting) to make PyInstaller compile the Python script WITH the gettext locales?

EDIT: I tried editing my palc.spec, here is the new version:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['palc.py'],
             pathex=['~/python-text-calculator'],
             binaries=[],
             datas=[('~/python-text-calculator/locales/*', 'locales')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='palc',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='palc')

And here is the output of the compiled package:

                                   Type 'help' to know the ton of Elive features available...
>>> ./palc
--------------------------------------------------------------------------
                          Language Selection
--------------------------------------------------------------------------
1 - English // Anglais
2 - Francais // French
Type: 1
Traceback (most recent call last):
  File "/Users/computer/python-text-calculator/palc.py", line 30, in <module>
    l_translations = gettext.translation('base', localedir='locales', languages=["en"])
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/gettext.py", line 514, in translation
    raise OSError(ENOENT, 'No translation file found for domain', domain)
FileNotFoundError: [Errno 2] No translation file found for domain: 'base'
[19393] Failed to execute script palc

This is the exact same output as it was without editing the palc.spec. Plus, it made the compiled package a directory (I ran ./palc inside the palc directory in dist), so I would still have to distribute a directory. What I need is a SINGLE FILE like the ones found here.

Can anyone help? Thanks! :D



from Compiling gettext locales with PyInstaller in Python 3.x

No comments:

Post a Comment