-->

PIL ImageTK not loading image in py2app applicatio

2019-04-28 18:18发布

问题:

I'm testing out an app that I've made which, amongst other things, loads a couple of .png images when opened. The images are displayed correctly on my Mac (10.7.5) and my mother's (10.8.5); however when my sister opens it on hers (10.9.5) the images don't load. All other functionality is otherwise intact. I should note that on my Mac and my mother's, I installed Python 3.4 and many of the packages that the app uses, including the PIL package, whereas my sister has none of these. The app was build using the command:

python3.4 setup.py py2app

Images are imported in the code with:

image = ImageTk.PhotoImage(file = "images/pic.png")

Setup file for py2app is as follows:

from setuptools import setup

APP = ['myapp.py']
DATA_FILES = [('', ['images'])]

OPTIONS = {'iconfile': 'myapp.icns', 'packages': ['PIL']}

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

My guess is that it's an issue with PIL, it just doesn't seem to want to play nicely with py2app. The reason I think it's PIL is because after running the command to build my app I get the following error message in Terminal.

Modules not found (conditional imports):
* Image (/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/py2app/recipes/PIL/prescript.py)

I'd be very grateful for any suggestions or direction.

回答1:

If you are building a python package that requires other packages to be installed, you can use the install_requires keyword within setup see docs. This has the additional benefit of installing the package(s) when the user runs pip install py2app. In your case I would use install_requires=['pillow'] and pip will automagically grab pillow during the installation process.