py2app with user packages

3.2k views Asked by At

I am working on a little cross-platform python project. I want to associate file extensions with the program, so for OSX it seems the script must become an Application. py2app seems like the perfect tool for the job, but I am having a lot of trouble finding good documentation on it.

My python script relies on a package I wrote (k.tk) and symlinked to /Library/Python/2.7/site-packages/k/tk (the individual files are symlinked into the folder - the folder is not a symlink).

This is the setup.py script I am using:

"""
This is a setup.py script generated by py2applet

Usage:
    python setup.py py2app
"""

from setuptools import setup
import sys

APP = ['randomizer.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True, 'includes': ['k.tk']}

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

I am having two problems with my setup, both of which I have found a really ugly workaround for that I do not like.

First Problem - Aliased Build

The first problem occurs when building with python setup.py py2app -A. This is supposed to build the application with links to the source files to make development easier. When I try to run the resulting application, however, it says

Import Error: No module named k.tk

The workaround for this is to append these lines to the python script:

import sys
sys.path.append("/Library/Python/2.7/site-packages/")

It seems like this implies that something is wrong with the python environment when the it is called from the application, but I don't know enough about py2app to know how to fix it.

Second Problem - Full Build

The second problem occurs when building with python setup.py py2app. This triggers a regular build and copies all libraries into the application. When I run this application, I get

OSError: [Errno 20] Not a directory: '[path snipped]/application.app/Contents/Resources/lib/python2.7/site-packages.zip/k'

The ugly workaround here was to open the application's files and extract site-packages.zip, into a folder called site-packages.zip. Now the path given in the error message is a directory, and the application runs fine.

Does anyone know how to fix this? I am feeling pretty stumped, and the only documentation I can find is this, which seems extremely limited.

Update

I tried copying /Library/Python/2.7/site-packages/k directory into the project directory. This fixed the Aliased Build, but unfortunately not the Full Build.

I also tried following the instructions in the Python Documentation (here) to properly package and install. This had the same effect. I am feeling quite frustrated; why would it be trying to look in a zip file as if it were a folder?

Update

I ended up writing a build script that just overwrites the zip with a directory with the same name. This solves the problem, but I am not happy with the solution.

I also have moved on to trying to build for Windows with py2exe. In Windows, I experience the exact same problem! This time library.zip is created and must be extracted for the program to run. Surely these programs do not just happen to have the same exact bug, but I cannot imagine why they both would incorrectly expect a zip file to be accessible the same way a folder is.

2

There are 2 answers

1
dashesy On BEST ANSWER

py2app needs a separate Python installation other than system Python for the bundle to work (I suggest HomeBrew). Also instead of includes you can try packages if k.tk is a package with more modules deeper in it.

0
Ivan Lavrenov On

If for some reason solution above does not work, try to add some const files that 3rd party packages open by open(*, 'r')

add what they want to open into data_files=[..]