MacOS – Including Python Script Dependencies (virtualenv contents) in Platypus

applescriptfindermacospython

I'm trying to turn a python script into a OS X App that you can run directly from Finder. I'm trying out Platypus, which seemed like the simplest option. The problem I'm running into is with the packages I'm including in the script. I installed them through pip into a virtualenv. Wondering what the best/most proper way to include those files in the Platypus bundle would be. I know I could just copy them over to the same directory and include them in the Platypus bundle, but would prefer to do something cleaner like including the virtualenv, although it seems like that would then have to be installed by the users of the app.

I also ran into py2app which seems to be able to achieve something similar to Platypus, but their documentation is somewhat obscure.

Best Answer

The way I've solved this is in the past is by bundling the contents of the virtualenv directory into Platypus and then activating the bundled Python interpreter with a script.

Here's a screencap of the Platypus interface to show you what I mean:

Platypus window setup

You'll notice that I've bundled the entire venv directory (which was created with the virtualenv command), and my Python script, my_app.py.

The script that I'm actually telling Platypus to run, run.sh, is a shell script with the following contents:

#!/bin/sh

"$PWD/venv/bin/python" my_app.py "$@"

All this script does is use the python binary in the venv directory to run the bundled Python script. The "$@" is there to pass any arguments to run.sh directly into my_app.py without changing them.