如何从 Objective-C 调用 PyObjC 代码?
可能的重复:
从 Objective-C 调用 Python
我是一名长期的 Python 程序员,但短期内时间可可程序员。我刚刚开始使用 PyObjC,完成工作是多么容易,真是令人惊奇。也就是说,我想尝试使用纯 ObjC 作为带有 PyObjC 模型的控制器。我可能会喜欢让 Python 成为 Python,让 Objective-C 成为 Objective-C。无论如何,我认为值得一试。
除了我无法弄清楚或找到任何关于如何从 Objective-C 调用 Python 的信息,只有相反的方式。有人可以向我指出这方面的任何资源吗? (也许它在 PyObjC 网站上,但我只是不知道我在寻找什么?)
编辑:在基本层面上,我最感兴趣的是能够调用 Python 模块并获取一些本机 ObjC 数据类型后退。
Possible Duplicate:
Calling Python from Objective-C
I'm a long-time Python programmer and short-time Cocoa programmer. I'm just getting started with PyObjC and it's really amazing how easy it it is to get stuff done. That said, I wanted to try using pure ObjC for my controller with PyObjC models. I might be enjoy letting Python be Python and Objective-C be Objective-C. I figured it was worth a try, anyways.
Except I can't figure out or find anything about how to call Python from Objective-C, only the other way around. Can someone point me to any resources on this? (Maybe it's on the PyObjC site but I just don't know what I'm looking for?)
Edit: I'm most interested, at the basic level, in being able to call a Python module and get some native ObjC data types back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种可能的方法。最诱人的是使用 py2app 从 Python 代码编译一个可加载包,您可以使用 NSBundle 访问主体类。不幸的是,这个用例并没有得到 py2app 开发人员的太多喜爱,而且我在 10.5 和 10.6 中发现了几个错误,包括将数据从 python 传回 Objective-C 时出现相当严重的内存泄漏。我不建议此时使用 py2app 。
第二种方法是反转嵌入。编写一个 Python cocoa 应用程序并在启动时从包中加载 Objective-C 代码(甚至在 main() 中)。如果您已经有一个大型 Objective-C 应用程序,这可能需要一些工作。据我所知,唯一的缺点是您无法在 Objective-C 代码中使用 GC,但这确实是使用 PyObjC 的普遍限制。
最后,您可以在 Objective-C 代码中实例化 python 解释器来加载 python 代码。这显然涉及更多,但如果您已经有一个大型 Objective-C 代码库并且想要向其中注入 Python 代码,这可能是最好的选择。 Xcode 中 Python-Cococa 应用程序模板中的 main.m 文件是开始查看此操作的好地方。
There are several possible approaches. The most tempting is to use py2app to compile a loadable bundle from your python code from which you can access the principal class using
NSBundle
. Unfortunately, this use case hasn't gotten much love from the py2app developers, and I've found several bugs in 10.5 and 10.6, including a rather nasty memory leak when passing data from python back in to Objective-C. I wouldn't recommend using py2app at thist point.The second approach is invert the embedding. Write a Python cocoa app and load your Objective-C code from a bundle at startup (even in main()). If you already have a large Objective-C app, this may take a bit of work. The only downside, that I'm ware of, is that you won't be able to use GC in your Objective-C code, but this is really a universal limitation in working with PyObjC.
Finally, you can instantiate a python interpreter in your Objective-C code to load your python code. This is obviously more involved, but may the best option if you already have a large Objective-C codebase into which you want to inject your python code. The main.m file from the Python-Cococa application template in Xcode is a good place to start to see this in action.
哎呀,我想我应该多搜索一下:
从 Objective-C 调用 Python
Whoops, guess I should've searched a bit more:
Calling Python from Objective-C