0

I'm trying to convert a python flet application into a macos application. I'm using the flet cli with the command flet pack.

The project files tree is:

PyFlutter
|_ assets
|  |_ fonts
|  |  |_font
|  |_ image.png
|_ backend.py
|_ credentials.log
|_ main.py
|_ icon.png

The command I used is:

flet pack "main.py" -n "AltExp-beta" --add-data "assets:assets" --add-data ".:backend.py" --icon "icon.png" --add-data ".:credentials.log"

The .spec file created is:

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


block_cipher = None


a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[('assets', 'assets'), ('.', 'backend.py'), ('.', 'credentials.log')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    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,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='AltExp-beta',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=False,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
    icon=['icon.png'],
)
app = BUNDLE(
    exe,
    name='AltExp-beta.app',
    icon='icon.png',
    bundle_identifier=None,
)

The files that are supposed to be created are created just fine, but when I try to execute the application it gives me an error:

Unable to proceed your request [Error]: "credentials.log" not found

But when I execute the unix executable file, everythings works fine.

I'll attach a demonstration:

Flet pack into .app not working
Edoardo Balducci
  • 332
  • 1
  • 10

1 Answers1

0

To get the resources in the macos app, you can use the following workaround:

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)

This functions basically return current working directory of your application, so you can get your files like resource_path('credentials.log') or resource_path('assets/image.png')

I guess the question is no longer relevant to you, but I faced the same problem and spent a lot of time to find a solution. Perhaps it could be useful to someone else )

Roman
  • 855
  • 1
  • 8
  • 15