Run python script using swift.

How can I run a python script (a .py file) using swift programming language from within xcode(iOS).

  • Can I use Peryton to code in Python and then switch to Swift language on 'Playgrounds' to develop an app?

Add a Comment

Replies

I’m not 100% sure I’m interpreting your post correctly, but my take is that you’re trying to run Python code inside an app running on iOS. That’s possible, but tricky. 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. That’s quite possible, but it involves a good understanding of the Python runtime.

Finally, you have to be careful about the origin of this Python code: App Review puts strict limits on your ability to download or execute code (see clauses 2.7 and 2.8) of the App Store Review Guidelines.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yes. I want to run python code inside an app. Could you please let me know how can I achieve that ?

I included a link to the Python doc that discusses this in my previous response.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

i'm interested in a similar solution but instead of wwithin an ios app, i'd like to execute python code from within a Swift package ( library or executable ). i would think that this is easier as it'd be run on a server or desktop inst of on ios device.


thank you in advance for any help or direction.

Using the embedded Python API is moderately tricky, mostly because the documentation is a bit sketchy, bordering on incomplete. Some heavy Googling may turn up relevant C++ examples, depending on what you are trying to do.


I've already done this with macOS, just using the system provided Python framework. But now looking at iOS, and of course the framework is not present. Searching around there is this short but useful technote here:


https://developer.apple.com/library/mac/technotes/tn2328/_index.html


And possibly a few publicly repo'd projects that might give examples, but I've only just started looking at these.

I've done this for macOS by using Python. Have a tutorial on it here: https://betterprogramming.pub/from-swift-import-python-f2fc2a997d4 I don't think iOS has a framework available.