pythonpython-3.xmacospyinstallerdylib

Pyinstaller on macos doesn't load library with --add-binary


I have a dev macOS VM and the builds work fine using pyinstaller 4.0.

As soon as I update pyinstaller anything other than 4.0, it fails to load my custom *.dylib files which are in the application folder when building pyinstaller.

I installed python using brew.

I am using this to build:

/usr/local/opt/python@3.8/bin/python3.8 -m PyInstaller --add-binary *.dylib:. --clean --windowed --onedir --noupx --name "$AppName" --icon=main.icns main.py

I have this that adds the program path to system PATH and remember this works with pyinstaller 4.0:

dllpath = os.path.dirname(os.path.realpath(sys.argv[0]))
if dllpath not in os.environ:
    os.environ["PATH"] += os.pathsep + dllpath

but as soon as pyinstaller is a version greater than pyinstaller 4.0, it would show cannot load library....

I have also tried installing the latest version of python and pyinstaller but having the same issue!

Any suggestions?


Solution

  • So in my case the dylib files are in the same path as the script.

    On macOS we would need to add "DYLD_FALLBACK_LIBRARY_PATH" env so the script would load the lib files.

    So before importing cairosvg, we would need to do this:

    import os
    import sys
    # load dlls used by cairo
    app_path = os.path.dirname(os.path.realpath(sys.argv[0]))
    os.environ["PATH"] += os.pathsep + app_path  # for Windows
    os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = app_path  # for macOS
    os.environ["LD_LIBRARY_PATH"] = app_path  # for Linux