Skip to content

Bundling Python apps

probonopd edited this page Apr 1, 2017 · 31 revisions

Due to the way Python imports work and due to the way Python packages are installed on Debian and Ubuntu, it is not trivial to create working AppDirs from them.

To find out what Python files a Python app (let's say "SpiderOak") accesses, you can use

strace -eopen -f ./SpiderOak 2>&1 | grep / | grep -v ENOENT | cut -d "\"" -f 2 | sort | uniq > openedfiles

Instead, it is advised that you use workingenv.py, a script that sets up an isolated environment into which you can install your Python app and its dependencies that are not part of the standard library.

wget http://svn.colorstudy.com/home/ianb/workingenv/workingenv.py
python workingenv.py MyNewEnvironment
cd MyNewEnvironment/
SITEPY=$(find -name site.py | head -n 1)
cat >> $SITEPY.new <<EOF
global USER_BASE, USER_SITE, ENABLE_USER_SITE
USER_BASE = None
USER_SITE = None
EOF
cat $SITEPY >> $SITEPY.new
mv $SITEPY.new $SITEPY
source easy_install bin/activate
cd src/
# wget ...
tar xzf * ; cd * ; python setup.py install
# or
easy_install *.egg

Then, in your AppRun file, set $PYTHONPATH to point at MyNewEnvironment/lib/python2.6

Please let me know if there is an easier way to bundle Python apps properly.

Miniconda

Miniconda is a version of Python that can run on multiple distributions. It might be a good idea to use that, because it is supposed to be isolated from the Python in the base system (Linux distribution). https://github.com/HelloZeroNet/ZeroBundle/ appears to be using this. Let us know if you try this.

Here is a script that installs FreeCAD using Miniconda:

https://github.com/looooo/FreeCAD_Conda/blob/master/utils/appimage/freecad_app_image.sh

AppImages converted from debs

If you convert a Python application from debs, then you should bundle Python inside the AppImage and make sure it does not load Python modules from the outside by the following trick:

We can do that by modifying usr/lib/python2.7/sitecustomize.py like this:

import sys,os
prefix = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(sys.path[0]))))
sys.path = [ prefix+s for s in sys.path if not s.startswith(prefix) ]

This works because sys.path[0] contains the directory from which Python was launched.

Avoid ctypes.util.find_library

The usual way that many wrapper libraries search for system libraries is via ctypes own function which on normal systems returns the .so filename. so in the case of VidCutter, for example:

import ctypes.util
sofile = ctypes.util.find_library('mpv')

the value of sofile should return as libmpv.so.1 but within the AppDir folder structure, and thus the resulting AppImage, this wouldn't work, returning None. It seems like ctypes.util.find_library isn't AppImage friendly (only searches outside of, but not inside the AppImage).You can work around it with a simple exception clause hardcoding the lib's filename like so:

sofile = ctypes.util.find_library('mpv')
if sofile is None:
    sofile = 'libmpv.so.1'

Please see https://github.com/ozmartian/vidcutter/issues/12 for more information.

Doing everything in Python

See https://github.com/benoit-pierre/plover/blob/9dc9fd5b2fb29b740eeb40f23d4a0630e8689b77/linux/appimage.sh for an example for how to create everything using Python, including downloading and building Python itself.

References

Also see https://notabug.org/themightyglider/Python_and_pygame_AppImage_tutorial