py2app TypeError: dyld_find() got an unexpected keyword argument 'loader'
Asked Answered
E

2

7

I'm having difficulty building my app using py2app. I can build it in alias mode without issue using this command:

python3.4 setup.py py2app -A

However when I try and build it using:

python3.4 setup.py py2app

I get the error message as per title of this post. From the research I've done I believe it's an issue with Pillow; however I need Pillow for this app. (Unless there's another module I can use to import images??). I've also tried cx_freeze without success.

Any help or direction much appreciated.

Full traceback as follows:

Traceback (most recent call last):
File "setup.py", line 19, in <module>
setup_requires=['py2app'],
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/core.py", line 148, in setup dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/dist.py", line 955, in run_commands self.run_command(cmd)
File"/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
distutils/dist.py", line 974, in run_command cmd_obj.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-  packages/py2app/build_app.py", line 659, in run self._run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 865, in _run self.run_normal()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 959, in 
run_normal self.create_binaries(py_files, pkgdirs, extensions,loader_files)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site- packages/py2app/build_app.py", line 1214, in create_binaries
platfiles = mm.run()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOStandalone.py", line 105, in run
mm.run_file(fn)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 84, in run_file
self.scan_node(m)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 110, in scan_node 
m =  self.load_file(filename, caller=node)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 93, in load_file
newname = self.locate(name, loader=caller)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOStandalone.py", line 23, in locate
newname = super(FilteredMachOGraph, self).locate(filename, loader)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/
site-packages/macholib/MachOGraph.py", line 49, in locate
loader=loader.filename)
TypeError: dyld_find() got an unexpected keyword argument 'loader'
Escudo answered 6/7, 2015 at 7:30 Comment(0)
R
6

This is a temporary solution, and it may break other things. I would recommend doing this is a virtual environment so you don't mess up your regular packages. This worked for me (virtenv is the name of my virtual environment)

Open the file /virtenv/lib/python3.4/site-packages/macholib/dyld.py and replace each instance of loader_path with loader. Now save and try again.

Rakish answered 23/9, 2015 at 23:6 Comment(6)
Thanks Michal for your post. Making those suggested changes allowed my program to be built without error. Though when I double-click my app, it doesn't work. If I attempt to debug by going to dist > (right-click) show package contents on the .app file > Contents > MacOS > my.app, Terminal says: Traceback (most recent call last): File "/Users/Name/Python programs/dist/BAWP.app/Contents/Resources /__boot__.py", line 67, in_recipes_pil_prescript import Image ImportError: No module named 'Image' I think the problem is that Pillow doesn't play well with py2app, but it's the only one I know of.Escudo
Yeah, the problem that caused me to stumble upon this was to do with matplotlib. I've also noticed that importing modules works a bit differently when built with py2app (Code which works normally throws import errors). How are you importing Image in your code? What happens if you try importing it in a different way.Rakish
At the moment I import the module with from PIL import Image, ImageTk. As far as I'm aware, there's only one way to import images. Unless I'm mistaken? … For a standard image import I use ImageTk.PhotoImage(file = "/Users/Name/image.png"), for gif images I use image = PhotoImage(data = b64_data) and for an image from a website I use pil_image = Image.open(data) then image = ImageTk.PhotoImage(pil_image) a few lines further down. Have you imported in any other way which didn't throw an error?Escudo
At first I used from tkinter import filedialog and then called filedialog.asksaveasfile() - this worked normally but threw and import error when running an app built by py2app. I changed it to from tkinter.filedialog import asksaveasfile and changed the call accordingly. This worked in both cases.Rakish
Just to add, at first I didn't think this was the solution as my app still crashed after py2app built it, but I figured out after that it was because I needed to include the PIL package in the setup.py file options. Normally this would work; however I've stumbled upon another issue with my images not loading which I've posted another question about: #33303418Escudo
Just a slight mod to ensure that both loader and loader_path can be used as args - my edits: def dyld_loader_search(name, loader_path=None, **kwargs): if loader_path is None and "loader" in kwargs: loader_path = kwargs["loader"] def dyld_find(name, executable_path=None, env=None, loader_path=None, **kwargs): """ Find a library or framework using dyld semantics """ if loader_path is None and "loader" in kwargs: loader_path = kwargs["loader"]Putdown
G
3

You can monkey-patch macholib rather than modifying it in site-packages. Place the following file to the directory containing setup_py2app.py and add import macholib_patch to the top.

macholib_patch.py:

"""
Monkey-patch macholib to fix "dyld_find() got an unexpected keyword argument 'loader'".

Add 'import macholib_patch' to the top of set_py2app.py
"""

import macholib
#print("~"*60 + "macholib verion: "+macholib.__version__)
if macholib.__version__ <= "1.7":
    print("Applying macholib patch...")
    import macholib.dyld
    import macholib.MachOGraph
    dyld_find_1_7 = macholib.dyld.dyld_find
    def dyld_find(name, loader=None, **kwargs):
        #print("~"*60 + "calling alternate dyld_find")
        if loader is not None:
            kwargs['loader_path'] = loader
        return dyld_find_1_7(name, **kwargs)
    macholib.MachOGraph.dyld_find = dyld_find
Gossamer answered 13/6, 2016 at 16:10 Comment(4)
While I don't get the error during build I see this Modules not found (conditional imports): * Image (/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/recipes/PIL/prescript.py)and app is not working. Did I miss something ?Anastomose
This seems to be answered here: stackoverflow.com/questions/30820906Gossamer
Same warning message but the script is working :) ThanksAnastomose
macholib.__version__ <= "1.7" isn't a great test when they get to version 1.10, 1.11 etc.Jellaba

© 2022 - 2024 — McMap. All rights reserved.