Call Python Code from an Existing Project Written in Swift

Call Python code from an existing project written in Swift

If anyone is ever interested in calling python from swift, here is some helpful material I found:

  • U the python framework - https://developer.apple.com/library/ios/technotes/tn2328/_index.html
  • PyObjC (a little more challenging) -
  • Cobbal - https://github.com/cobbal/python-for-iphone
  • Python docs (you would need to make C-Swift bridge)

Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

Import Python code and library in Swift

Best bet is probably going to be the python framework: https://developer.apple.com/library/ios/technotes/tn2328/_index.html

Also check this out, very similar to your question: How to call Python code from an existing iOS project written in Swift

Swift receive string and run it in Python

there are several ways to do that .
You can invoke the system interpreter subprocess.run(...) with option -c and pass the commands as string.

You can also save the string to a file and execute it with the system interpreter.

I can't use Python module in Swift

A year later, I got a solution.
The name of module was wrong.

import PythonKit
let np = Python.import("numpy")

I should have installed on this way and implemented this way exactly.

Thanks regards!

Xcode: 11.3.1

Swift: 4.2

Toolchain: Swift for TensorFlow 0.8 Release

Embed python in to iOS (iphone) app written in Objective-C/Swift/C/C++ (whatever language i can compile in Xcode and bridge to iOS)

If anyone is ever interested in calling python from swift, here is some helpful material I found(used) -

  • use the python framework - https://developer.apple.com/library/ios/technotes/tn2328/_index.html
  • PyObjC (a little more challenging) -
  • cobbal - https://github.com/cobbal/python-for-iphone
  • python docs (you would need to make C-Swift bridhe)

Most of it is for Objective-c, but if you need to use swift you can easily just create an ObjC-Swift bridge (super-super easy) - Lookup the apple docs

I managed to get the first two methods working on an OS X app, but I got some architecture errors for iOS(not important). I've pushed the project aside for now, I have a ton I need to get done.

Hope this will be helpful for anyone interested :-)



Related Topics



Leave a reply



Submit