Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Grinnell Appdev
23,708 PointsHow to add and call Python code from an existing iOS project written in Swift
I need a way to call python code from swift, a library would be ideal. I've done a considerable amount of google searching, and the closest material I found is for objective-c. (posted by Shaun)
5 Answers
Grinnell Appdev
23,708 PointsIf 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 :-)
-Shaun
Tyler Amend
2,236 PointsPlease let me know if it works I would be curious to hear how it plays out.
Caleb Kleveter
Treehouse Moderator 37,862 PointsAre you trying to write python code in a Swift file?
Grinnell Appdev
23,708 PointsNo, I have an engine written in python that took me a while to build. Instead of rewriting the whole thing in swift, I just want to make method calls from swift to the python engine.
Caleb Kleveter
Treehouse Moderator 37,862 PointsSorry, I don't know of any way to do that.
Tyler Amend
2,236 PointsI don't know that you can given Python and Swift are not completely compatible. I wonder if you could create a wrapper of sorts? I know you could import C++ into a Swift file if you create an objective-C wrapper for it, but than again those are C based languages.
Grinnell Appdev
23,708 PointsThanks for the hint Tyler. I'm going to try to create a C wrapper and see if that works.