Search code examples
pythonpython-2.7py2app

Py2app issue: python binary does not have a shared library (or framework) at all


I am trying to build a distribution for a script. I have used py2exe to create an exe file and it works fine. I am trying to use py2app to create something similar for Mac. However, I am getting this error when I use the command

python setup.py py2app

Error Message: python binary does not have a shared library (or framework) at all

Any idea why this is the case? I am importing some modules like BeautifulSoup apart from the standard ones like urllib, math in the main script file I am running the entire thing on a 64-bit Windows machine running python 2.7


Solution

  • You need to run py2app on a Mac.

    The py2app code uses the copy of Python that's being used to run it to build the standalone executable. If that Python isn't a Mac build (which it won't be, if you're running on Windows), it won't be able to create a Mac executable.

    Technically, the error message is telling you that it can't find the libpython.dylib or Python.framework associated with sys.executable, which is true, but could be more useful in this case.


    There are alternatives to py2app, like cx_freeze, but they all work the same way: building an executable out of the Python installation used to run them.

    So, if you want to build a Mac executable on Windows, there's no automated way to do it.

    But there are a few possibilities.

    First, you can buy a used Mac Mini for probably $100 or so. Get it set up for development, turn on Remote Login access (in the Sharing pane of Preferences) and leave it running in the corner. From Windows, you can use a little 4-liner ssh script to tell the Mac to check out the source, py2app it, zip up the result, and scp it over to the Windows box (or copy it via Windows file sharing, or ftp it, or check it into source control, or whatever).

    If that's not feasible for some reason, hopefully you can at least get access to a Mac once in a while. (If not, how are you ever going to test things?). If so, you can build an app with py2app and zip it up to use as a template. Each time you want to make a new build, you can do that on Windows, just by modifying what's in the template.

    The main foo.py script goes in foo.app/Contents/Resources/foo.py. Any other Python modules (whether standard-library, third-party, or your own code), with a few exceptions, go into the fake-standard-library zipfile in foo.app/Contents/Resources/lib. Any C extension modules go into a directory like foo.app/Contents/Resources/lib/python2.7/lib-dynload.

    As long as you don't modify any C extensions, upgrade to a new version of Python, or add new third-party libraries that you don't know how to install manually (e.g., because you got them as an egg via easy_install), this will continue to work. If you do any of those things, you'll need to go back to the Mac and create a new template with py2app.

    If you don't have access to a Mac at all, you may be able to find a pre-built py2app-generated app for some other project that happens to contain everything you need, in which case you can use it as a template. But this is a huge stretch.