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.12k stars 161 forks source link

how to use python in existing ios app? #201

Closed heygarvish closed 8 months ago

heygarvish commented 8 months ago

What is the problem or limitation you are having?

i've mainly this problems:

  1. how can i use python's 3rd party libraries like spotipy inside (embedded) my existing ios app?
  2. how to use briefcase to add python and it's 3rd party libraries into my existing ios app?
  3. how can i update 3rd party python libraries at runtime (inside from swift code)?
  4. can app store reviewers be able to see what python libraries i'm using in my app?

Describe the solution you'd like

as i'm a newbie to ios development, i don't know much about it.

Describe alternatives you've considered

i've seen this SPM library:

https://github.com/kewlbear/YoutubeDL (they are able to update the python libraries at runtime)

Additional context

i've read the usage guide, but still don't get how to add 3rd party python libraries. i mean how should i compile (or whether to compile or not) and how to get it's path (my macos path or ios app asset path).

freakboy3742 commented 8 months ago

Q1: The simple answer is "use Briefcase" - in which case, you add the requirement to your requires list, and the third-party library will be installed. However, that is dependent on the library either being pure Python, or being on the list of supported iOS third party libraries. In this case, it's hard to judge if spotipy will work - spotipy itself is pure python, but it has a dependency on Redis, which suggests that you need to have Redis available as a network service - which you won't have on iOS.

If you're not using Briefcase, you need to manually install the libraries into whatever directory you're using for your site ackages, and perform post-processing on the binary libraries. We don't officially support non-Briefcase usage; we provide a usage guide for non-Briefcase usage, but those instructions are provided as-is. It's up to you to work out how to adapt those instructions to your iOS app.

Q2: If you want to learn how to use Briefcase, start with the BeeWare tutorial; that covers everything you need to know.

Q3: Adding requirements at runtime isn't something we officially support, because it is in a potentially hazy area with the iOS App Store guidelines. If we did* support it, we would only support it from within the BeeWare suite of tools. Usage from a Swift codebase would be "off label" usage in the same category as using the support package outside Briefcase.

FWIW: it can be done - you download the wheel file for the package you want to add, unpack it into your app's PYTHONPATH. However, it's up to you to work out how this can be done. It will also only work for pure-python requirements - you can't update binary requirements at runtime on iOS, due to binary signing requirements.

Q4: App store reviewers have access to the full app bundle, and Python code is transmitted "in the clear"; so a reviewer (or, for that matter, any user who has access to your app and sufficient technical knowledge to inspect the app bundle) would be able to work out what libraries are in use. Whether they App Store reviewers actually do this is unclear - App Store review processes are entirely opaque.

Closing the ticket as this is a question, not an enhancement request; if you have any follow up questions, feel free to respond to this ticket.

heygarvish commented 8 months ago

thanks for your quick reply.

i will start with the BeeWare tutorial then.

closing this as you answered all my questions.