py2app built app displays `ERROR: pygame.macosx im

2019-03-11 12:45发布

问题:

Trying to build an app on the Mac using py2app. Got everything working fine on my machine, but when moving the app to another, it crashes and the console displays this error.

ERROR: pygame.macosx import FAILED

Anybody have a solution to this?

回答1:

Found the problem and solution after many hours. Turns out other people have experienced similar problems and their articles were quite helpful:

http://b.atcg.us/blog/2010/04/13/py2app-hell-the-first.html

http://www.vijayp.ca/blog/?p=62

In case someone else runs into the issue, this particular problem was caused because the Python framework was not being bundled into the application. You can confirm this by right-clicking your app to view package contents, then proceed to Contents/Frameworks/. If Python.framework is not there, it should be.

Be sure to download Python - My first issue was reliance on Apple's build in Python package. Don't use this. You need to install your own version of python. Go to http://www.python.org/download/releases/, find a version (I stuck with 2.6), download the gzip (not the mac package), and install with the following if you are running Snow Leopard:

./configure --enable-framework MACOSX_DEPLOYMENT_TARGET=10.6 --with-universal-archs=intel --enable-universalsdk=/
make
sudo make install

Adjust Paths, Install Packages - From here there you need to adjust your paths to ensure you are using your custom-installed version. From here, I reinstalled the following packages - this turned out to be a dependency nightmare so I'm including the version numbers as well:

  • py2app 0.5.2
  • macholib 1.3
  • modulegraph .8.0

If these packages actually worked, you should be able to build and run your app now. Unfortunately, they don't. I'll go into the errors and my hacked solutions in a bit, but there's some settings in the build file that need to be made first.

First the setup.py file should like a little somethin' like this:

setup.py

from setuptools import setup

APP = ['Game.py']
DATA_FILES = ['data']

OPTIONS = {
    "argv_emulation": False,
    "compressed" : True,
    "optimize":2, 
    "iconfile":'data/game.icns',        
}

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

then to be extra safe, I use a shell script to call this.

build.sh

## Remove previous builds.  Start with clean slate.
rm -rf build dist

## Force python into 32 bit mode.
export VERSIONER_PYTHON_PREFER_32_BIT=yes

## Force build with custom installed python
/Library/Frameworks/Python.framework/Versions/2.6/bin/python setup.py py2app

Running build.sh should compile the app. If it does not compile, I have good news -- it's not your fault. Due to glitches in the libraries, you may run into some (or all) of the following:

Potential Problems If the build script fails, scan the traceback for some of the following keywords:

pygame not found - basic path problem in py2app. Add...

sys.path.insert(0, os.path.join(os.getcwd(), 'lib', 'python2.6','lib-dynload')) ## Added to fix dynlib bug

after the import statements in boot_app.py in the py2app lib.

pythonNone - This appears to be a bug in the macho package where it cannot determine the version number of your python build. To solve this, I added the following lines to build_app.py in py2app.

## Add these two lines...
if not info["version"]:
  info["version"] = "2.6"
## Before this line. (line 941 in method copy_python_framework() at time of writing)
pydir = 'python%s'%(info['version'])

No such file or directory...Python.framework/[lib|include] - py2app is simply looking for directories that exist deeper in the file system tree. Go to the Python.framework directory and symlink up the place...

cd /Library/Frameworks/Python.framework
sudo ln -s Versions/Current/include/ include
sudo ln -s Versions/Current/lib lib

That should do it! - These steps created a compiled app that worked on other intel machines.



回答2:

Thank you for posting what you found!

I had a similar problem. I tried various combinations of what you suggested, and isolated the single issue for me to be the bug in boot_app.py which you identify above.

Once I added the one-line fix to boot_app.py which you identify above, everything worked, even using the pre-installed Apple build of python (version 2.6.1).

I should note that when I say "everything worked," I really mean building a py2app app for actual distribution, i.e. using the normal command:

python setup.py py2app

The "alias" mode. i.e.

python setup.py py2app -A

which the py2app documentation suggests for use during development, still does not work for me (with the same module not found error). But better the actual distribution build working than nothing at all! Again, thanks.