Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run python program in IOS Swift app

I’m an amateur coder working on a project right now, and I was wondering if you could help me.

Basically, I’ve been doing a lot of research on a particular algorithm that I find runs much faster using Python. The goal is to get this Python code to run in my IOS application. What I’ve found is that the Python interpreter has to be embedded with the Swift application in order to do this. However, I haven’t found much useful documentation or other sources on how to do this. Do you have any suggestions or advice?

like image 968
Aaron Mednick Avatar asked Oct 10 '17 01:10

Aaron Mednick


People also ask

How do I run a Python file in Swift?

Yes, you can run python code from swift by using the PythonKit, a framework based on the Python module from the Swift for TensorFlow project. It's important to note that Python is not available on iOS. But you can build pretty awesome utility apps for macOS and Linux.

Can I run Python code as an iOS app?

iOS does not have Python pre-installed, so you'll have to embed a copy of Python within your app. Additionally, iOS apps can't run arbitrary secondary processes, so you'll have to run the Python interpreter inside your app's process.

Can you use Swift and Python together?

Swift For TensorFlow supports Python interoperability. You can import Python modules from Swift, call Python functions, and convert values between Swift and Python.


1 Answers

This tutorial seems to be fairly clear and easy to follow, and cover the necessary steps. Is there any chance you could share what the algorithm in question is? It might make more sense to use C - it looks like you can mix Obj-C with Swift. That said, I am not confident that you can actually do this on iOS.

First of all, your app might not get through App Store review if it has an executable for Python in it. Apple has its own docs on embedding Python in OSX/Mac OS apps, but not for iOS.

Additionally, I don't think an official executable of Python for iOS even exists. The closest thing I could think of, or find, is Pythonista, but I believe the developer made their own version of Python for iOS.

It looks like you can export Pythonista code to stand-alone apps, although how useful this is, and weather or not you can do this for iOS, I don't know. Additionally, that feature only works with Python2. Hope this helps.

like image 171
Max von Hippel Avatar answered Oct 17 '22 04:10

Max von Hippel