Packaging apps
February 23, 2022 —
~kai
Packaging apps is useful for running apps on multiple machines without worrying about versions.
Here is a short guide on packaging Python apps, in 20 steps. It first involves “freezing” it using PyInstaller, then bundling it into an AppImage using AppImageKit’s appimagetool.
- Enter your app’s source directory, all following operations occur
from this directory. Create a Conda environment with
conda create --name myapp
- Install the necessary dependencies for your app to work
- Install PyInstaller with
conda install pyinstaller
- Set up variables
APP=APPNAMEHERE; LOWERAPP=${APP,,}
replacing APPNAMEHERE with your app name, like SCRIPT - Backup your entry point script and name it LOWERAPP.py, like script.py
- Set up the app for freezing using
pyinstaller script.py
- This creates
script.spec
, abuild
folder, and adist
folder - Run
pyinstaller script.spec
to create the Unix executable. Thescript.spec
file customizes the freezing process cd dist/script
to find the resultant files, locate the executable, and test it works using./script
- It may fail if external data files were needed
- If there were data files, it should have been specified in
script.spec
- Otherwise, you can also just copy necessary data files into the dist
folder,
cp -R * dist/script
- When it works,
cd ..
- Helper functions can be obtained and sourced by
wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
source ./functions.sh
- Create the AppDir using
mkdir -p $APP/$APP.AppDir/usr/bin/
, the specification of AppDir is here - Enter the AppDir with
cd $APP/$APP.AppDir
, all following operations occur from this directory - Copy the contents of step 9 into usr/bin,
cp -Rv appsource/dist/script/* usr/bin/
#!. untested - Make sure it is executable,
chmod a+x usr/bin/$LOWERAPP
- Get the AppRun file,
get_apprun
- In the AppDir, create a desktop entry file
vim $LOWERAPP.desktop
with the format:
[Desktop Entry]
Name=Lector
Comment=Read e-books
Type=Application
Terminal=false
Exec=lectormain
Icon=lectormain
Categories=Utility;Qt;Graphics;Viewer;
- Obtain 128x128 or 256x256 PNG icon, and save it as
.DirIcon
and$LOWERAPP.png
(both are needed), in the desktop entry, the Icon= field should have $LOWERAPP but without the extension - Bundle dependencies using
copy_deps; copy_deps; copy_deps
and thendelete_blacklisted; move_lib
- Check the app is runnable using
./AppRun
- When it works,
cd ..
, all following operations will occur from this directory - Obtain the appimage tool and mark it as executable
wget "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage"
chmod a+x appimagetool-x86_64.AppImage
- With the AppDir in place, you can run
./appimagetool-x86_64.AppImage APPNAMEHERE.AppDir/
tags: code