macos-notifications icon indicating copy to clipboard operation
macos-notifications copied to clipboard

Library not working in frozen application (e.g. with PyInstaller)

Open MShekow opened this issue 2 years ago • 5 comments

I'm trying to build a CLI/Terminal app (frozen with PyInstaller), and it seems that the macos-notifications library starts a new process that calls my CLI again, but with unexpected parameters. Is there any way to work around this problem?

MShekow avatar Sep 06 '23 10:09 MShekow

Hi @MShekow,

Sorry to hear that. Could you provide a minimal example so I can try to replicate it? I would expect it to work if you wrap all your code around in an if __name__ == "__main__" check

Jorricks avatar Sep 06 '23 17:09 Jorricks

Sure.

main.py:

import argparse

from mac_notifications import client

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("foo")
    parser.add_argument("bar")
    parser.add_argument("baz")
    args = parser.parse_args()
    client.create_notification(title="foo", text="bar")

pyinstaller.spec:

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

block_cipher = None

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[],
    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,
    [],
    exclude_binaries=True,
    name='testapp',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='testapp',
)

Run pip install pyinstaller macos-notifications, then just freeze the app with pyinstaller pyinstaller.spec, then run the app with ./dist/testapp/testapp 1 2 3.

Console printout:

usage: testapp [-h] foo bar baz
testapp: error: the following arguments are required: bar, baz
usage: testapp [-h] foo bar baz
testapp: error: the following arguments are required: baz
multiprocessing/resource_tracker.py:104: UserWarning: resource_tracker: process died unexpectedly, relaunching.  Some resources might leak.

Also, very funny, I dare you to instead use the following main.py (if you want to see a logic bomb in action):

from mac_notifications import client

if __name__ == '__main__':
    client.create_notification(title="foo", text="bar")

It gives you a good impression of how it feels to hard-reboot your mac :S

MShekow avatar Sep 06 '23 18:09 MShekow

Any updates on this? Running into the same issue, I'm expecting a notification but the app just starts a new instance

technoluc avatar Jan 28 '24 12:01 technoluc

Thanks again for raising this issue. Unfortunately, I don't have any updates on this. I am not really aware of the PyInstaller setup and therefore wouldn't know what is breaking it. I am not convinced this is necessarily because of the macOS-notification library, but because of a slightly different Python setup. Therefore, I will keep this issue open to allow anyone to investigate (and find) the issue. However, I hope you can respect my decision to not investigate this myself.

Jorricks avatar Feb 04 '24 14:02 Jorricks

It seems to happen on any frozen python application

furtabs avatar Feb 29 '24 02:02 furtabs