beeware / Python-Apple-support

A meta-package for building a version of Python that can be embedded into a macOS, iOS, tvOS or watchOS project.
MIT License
1.11k stars 160 forks source link

Include Python 3.7 + external library in a AppStore compatible MacOS app #82

Closed flocked closed 3 years ago

flocked commented 4 years ago

Hi, I want to create a App Store compatible MacOS app that uses this Python library. https://github.com/deezer/spleeter

The library requires Python 3.7, therefor I can’t use the Python version that is included in MacOS. The library itself also has requirements.

I plan to use Swift for all the UI and most of the code. I only need to process files with the Python library.

What would be the best way to proceed? Any hints would be great. Thanks!

freakboy3742 commented 4 years ago

Your best option is to look into Briefcase. Briefcase is a tool that will convert your Python project into a standalone macOS app, including all it's dependencies. It uses the support library produced by this project to provide Python as part of the distributed binary.

The resulting output is a standard macOS .app file. You will need to manage code signing and notarization before submitting the app to the App Store; automating this last step is something we're aiming to add in our upcoming Briefcase 0.3 release.

flocked commented 4 years ago

Thanks.

How do I provide the UI with Briefcase?

The python library doesn't have any interface and so far I also didn't create a Python project. My ideal situation would be to create a standard Swift Mac OS application with Xcode & Storyboard, that uses in some parts the Python library (for processing audio files).

freakboy3742 commented 4 years ago

Ok - in which case, you should be able to use this library, but you're going to be somewhat on your own. What you're doing is somewhat simliar to what Briefcase does to package iOS applications; you add the Python libraries as an embedded Python instance, and start the Python runtime as part of the application startup.

You can see an example of this by looking at the iOS Briefcase template (or by deploying an iOS application with briefcase). The interesting part is all in main.c - setting up the Python interpreter, and invoking the main script.

That code uses Objective C rather than Swift, though; I haven't done any work with Swift, so I can't comment on what would need to change to adapt to the new language.

Beyond that, you're somewhat on your own. I can't point you at a good example of this in use; the use cases of the BeeWare project are focused on using Python as the primary language.

dgelessus commented 4 years ago

For the "calling Python from Swift" part: TensorFlow has native support for Swift and includes Python interoperability features, which should allow calling your Python library from a Swift-based application.

According to the documentation, the interop library uses system-wide Python installations by default, but an environment variable can be used to select a different Python installation. If you can build an embedded Python distribution using the approach that @freakboy3742 suggested and include it in your app, the interop library should be able to use that Python distribution instead of the system Python.

flocked commented 4 years ago

@dgelessus @freakboy3742 Thanks. I got it working with PythonKit

PythonKit is based on the Python module from the Swift for TensorFlow project.

I use this prebuilt Python framework with PythonKit and bundle it inside my app.

https://s3-us-west-2.amazonaws.com/pybee-briefcase-support/Python-Apple-support/3.7/macOS/Python-3.7-macOS-support.b1.tar.gz

I also simply copied all required packages from my default site-packages folder to my app.

Will it cause problems to include the prebuilt Python framework with the AppStore/Sandboxing?

Thanks for your help.

freakboy3742 commented 3 years ago

Closing as the original question has been answered.