pythonmacospyqtpy2app

Python standalone application failing when launched (macOS)


So I have made this simple Password Manager in Python 3.8 using PyQt5, and I have been using the 'py2app' module to create a standalone application of this program. However, when I execute my app (both from the terminal and the finder), I get a simple popup saying "keysafe Error". My code is executing just fine and has no errors. I can't seem to figure out the problem here. If it helps, I get this message in my macOS logs:

Mar 16 10:29:01 my-MacBook-Air com.apple.xpc.launchd[1]: Coalition Cache Hit: app<application.org.pythonmac.unspecified.keysafe.5854321.5857989(501)> [78372]
Mar 16 10:29:01 my-MacBook-Air keysafe[16934]: keysafe Error
Mar 16 10:29:02 my-MacBook-Air com.apple.xpc.launchd[1] (application.org.pythonmac.unspecified.keysafe.5854321.5857989[16934]): Service exited with abnormal code: 255

I have also been using the following terminal commands to generate my standalone application using py2app:

cd <path_to_my_python_script>
py2applet --make-setup keysafe.py
python3 setup.py py2app

I really can't figure out what to do, and all help is appreciated! Thanks in advance!

Update: If you go to your application and right-click it in the finder, it will give you a menu option. Then, click Show Package Contents. After that, navigate to the Contents > MacOS and you will see a Unix Executable File of your python script. Run the file, and you can locate what your error is.
After trying this out, I got an error with problems about the function I had named exit in my password manager. I thought that the keyword exit might conflict with some sort of MacOS Setting, and changed the function name to exit_. After creating another Standalone Application with my updated script, I ran the Unix executable again and got a new error (so the exit keyword was fixed). My new error is:

Traceback (most recent call last):
  File "{path}/Password Manager/dist/keysafe.app/Contents/Resources/__boot__.py", line 110, in <module>
    _run()
  File "{path}/keysafe.app/Contents/Resources/__boot__.py", line 84, in _run
    exec(compile(source, path, "exec"), globals(), globals())
  File "{path}/Password Manager/dist/keysafe.app/Contents/Resources/keysafe.py", line 1, in <module>
    from cryptography.fernet import Fernet
  File "<frozen zipimport>", line 259, in load_module
  File "cryptography/fernet.pyc", line 16, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "cryptography/hazmat/primitives/padding.pyc", line 11, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "cryptography/hazmat/bindings/_padding.pyc", line 14, in <module>
  File "cryptography/hazmat/bindings/_padding.pyc", line 10, in __load
  File "imp.pyc", line 342, in load_dynamic
ModuleNotFoundError: No module named '_cffi_backend'
2021-04-04 10:30:46.115 keysafe[27312:1337934] keysafe Error
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Solution

  • So I finally was able to find an answer to this question!
    The first thing to do is open your setup.py file, the default one should look like so:

    """
    This is a setup.py script generated by py2applet
    
    Usage:
        python setup.py py2app
    """
    
    from setuptools import setup
    
    APP = ['my_program.py']
    DATA_FILES = []
    OPTIONS = {}
    
    setup(
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )
    

    Then, change the OPTIONS = {} to include each one of your imports

    OPTIONS = {'includes': ['cryptography.fernet', 'PyQt5.QtWidgets',
                            'PyQt5.QtGui', 'PyQt5.Qt', 'PyQt5', 'PyQt5.QtCore', '_cffi_backend']}
    

    This manually imports your modules, and this should work if you were getting any similar errors!