IronPython 与 Python .NET
我想从 Python 代码访问一些用 C# 编写的 .NET 程序集。
一些研究表明我有两个选择:
- IronPython 具有 .NET 接口功能/支持内置
- Python Python .NET 包
两种解决方案之间的权衡是什么?
I want to access some .NET assemblies written in C# from Python code.
A little research showed I have two choices:
- IronPython with .NET interface capability/support built-in
- Python with the Python .NET package
What are the trade-offs between both solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您想主要将代码基于 .NET 框架,我强烈推荐 IronPython 与 Python.NET。 IronPython 几乎是原生的 .NET - 因此它在与其他 .NET 语言集成时效果很好。
如果您只想将 .NET 中的一两个组件集成到标准 Python 应用程序中,Python.NET 是不错的选择。
使用 IronPython 时存在显着差异 - 但其中大多数都相当微妙。 Python.NET 使用标准 CPython 运行时,因此此Wiki 页面 是对两种实现之间差异的相关讨论。 最大的差异发生在异常的成本上 - 因此一些标准 python 库由于其实现而在 IronPython 中表现不佳。
If you want to mainly base your code on the .NET framework, I'd highly recommend IronPython vs Python.NET. IronPython is pretty much native .NET - so it just works great when integrating with other .NET langauges.
Python.NET is good if you want to just integrate one or two components from .NET into a standard python application.
There are notable differences when using IronPython - but most of them are fairly subtle. Python.NET uses the standard CPython runtime, so this Wiki page is a relevant discussion of the differences between the two implementations. The largest differences occur in the cost of exceptions - so some of the standard python libraries don't perform as well in IronPython due to their implementation.
虽然同意 Reed Copsey 和 Alex Martelli 给出的答案,但我想指出另一个区别 - 全局解释器锁 (GIL)。 虽然 IronPython 没有 GIL 的限制,但 CPython 有 - 因此,对于那些 GIL 成为瓶颈的应用程序(例如在某些多核场景中),IronPython 比 Python.NET 具有优势。
来自 Python.NET 文档:
另一个问题是 IDE 支持。 目前,CPython 可能比 IronPython 具有更好的 IDE 支持 - 因此这可能是选择其中一个的一个因素。
While agreeing with the answers given by Reed Copsey and Alex Martelli, I'd like to point out one further difference - the Global Interpreter Lock (GIL). While IronPython doesn't have the limitations of the GIL, CPython does - so it would appear that for those applications where the GIL is a bottleneck, say in certain multicore scenarios, IronPython has an advantage over Python.NET.
From the Python.NET documentation:
Another issue is IDE support. CPython probably has better IDE support at present than IronPython - so this may be a factor in the choosing of one over the other.
大多数依赖 CPython C-API 的科学和数值 Python 库(numpy、scipy、matplotlib、pandas、cython 等)大多在 CPython 下工作,因此在这种情况下,您最好的选择是 pythonnet(其他名称 - Python.NET)和用于 .NET 的 Python)。
对于 WxWidgets、PyQt/PySide、GTK、Kivy 等 CPython GUI 绑定也是如此,尽管 pythonnet 和 IronPython 都可以使用 WPF 和 WinForms。
最后,IronPython 尚未完全支持 Python 3。
Most of scientific and numerical Python libraries that rely on CPython C-API (numpy, scipy, matplotlib, pandas, cython, etc.) are working mostly under CPython, so in that case your best bet is pythonnet (other names - Python.NET and Python for .NET).
The same is true for CPython GUI bindings such as WxWidgets, PyQt/PySide, GTK, Kivy, etc., although both pythonnet and IronPython can use WPF and WinForms.
And finally IronPython does not fully support Python 3 yet.
IronPython 是“.NET 原生”——因此,如果您希望将 Python 代码与 .NET 完全集成,那么它会是更好的选择; Python.NET 与经典 Python 配合使用,因此它可以让您的 Python 代码与 .NET 保持一定的距离。 (请注意,通过 此代码,您实际上可以在 IronPython 中使用为 CPython 编写的扩展代码,因此这不再是歧视条件)。
IronPython is ".NET-native" -- so it will be preferable if you want to fully integrate your Python code with .NET all the way; Python.NET works with Classic Python, so it lets you keep your Python code's "arm's length" away from .NET proper. (Note that with this code you can actually use extensions written for CPython from your IronPython code, so that's not a discriminating condition any more).
IronPython 来自 Microsoft,所以我会凭自己的直觉首先使用它,因为您必须假设它与其他 MSFT 技术配合得更好。
IronPython comes from Microsoft, so I would go with my gut and use that one first since you have to assume it will play nicer with other MSFT technologies.
至于 2016 年。
在我的公司,我们使用 IronPython,但我们对性能不满意(主要是内存使用 - 垃圾收集器太慢),因此我们决定切换到标准 Python 并使用 Zeroce-s ICE 将其与 .Net 集成。
As for 2016.
In my company we used IronPython, but we were not satisfied with performances (mostly memory use - garbage collector was too slow) so we decided to switch to standard Python and integrate it with .Net using Zeroce-s ICE.
IronPython 目前不支持
IronPython 3 中的 Python 3.6(仅 2.7)< em>“尚未提供 IronPython 3 的版本。”
IronPython, currently, doesn't support Python 3.6 (only 2.7)
from IronPython 3 "Builds of IronPython 3 are not yet provided."
Ironpython 与 C# 类似,它依赖于静态预构建库,而与 C# 不同,它是一种动态语言。
Ironpython 与 C# 类似,又依赖于静态预构建库,而与 C# 不同,它是
Cpython 就像 C++ 一样,Ironpython 是一种动态语言,可以访问动态库,这反过来又意味着被迫编写所有内容。
Cpython 就像 C++ 一样,
Ironpython 在某些方面比 C# 更快,但不比 Cpython 快,但是您可以将 Ironpython 链接到任何语言,从而克服即将出现的问题,但您也可以对 Cpython 做同样的事情。
无论您选择什么,都是一种有趣、简单且功能强大的语言!
Ironpython is like C# in turn it relies on static prebuilt libraries while unlike C# is a dynamic language.
Cpython is like C++ like Ironpython is a dynamic language and has access to dynamic libraries which in turn translates to being forced to write everything.
Ironpython is faster than C# in certain areas but not faster than Cpython, however you can link Ironpython to any language thus over coming problems but then again you can do the same with Cpython.
A funny, simple and powerful language regardless what you choose!
Iron Python 基本上是集成了 .net 支持的 Python 2.7,它可能永远不会支持 Python 3。它失去了 C 和 Python 库,但在扭曲方面可以访问 .net 并可以使用 C# 进行扩展。 因此,如果您已经使用 C#,那么 Iron Python 是一个额外的好处。
Iron Python is basically Python 2.7 with integrated .net support it probably will never support Python 3. It loses out on C and Python libraries, however on the twist side has access to .net and can be extended with C#. So if you use C# already then Iron Python is a bonus.
我主要更喜欢 Python for .NET,因为 IronPython 被编译为托管代码,可以轻松反编译(这是我最讨厌的),但是使用 py2exe 或 pyinstaller,您可以将带有 NET 模块的 Python 编译为非托管应用程序。
I mainly prefer Python for .NET, because IronPython is compiled as managed code, which can be easily decompiled (what I most hate), but with py2exe or pyinstaller you can compile Python with NET module as an unmanaged application.