IronPython 与 Python .NET

发布于 2024-07-29 01:38:18 字数 307 浏览 7 评论 0原文

我想从 Python 代码访问一些用 C# 编写的 .NET 程序集。

一些研究表明我有两个选择:

两种解决方案之间的权衡是什么?

I want to access some .NET assemblies written in C# from Python code.

A little research showed I have two choices:

What are the trade-offs between both solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

痴情 2024-08-05 01:38:18

如果您想主要将代码基于 .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.

习ぎ惯性依靠 2024-08-05 01:38:18

虽然同意 Reed Copsey 和 Alex Martelli 给出的答案,但我想指出另一个区别 - 全局解释器锁 (GIL)。 虽然 IronPython 没有 GIL 的限制,但 CPython 有 - 因此,对于那些 GIL 成为瓶颈的应用程序(例如在某些多核场景中),IronPython 比 Python.NET 具有优势。

来自 Python.NET 文档:

嵌入器的重要说明: Python
不是自由线程并且使用全局
解释器锁定以允许
多线程应用程序
与 Python 安全交互
口译员。 更多信息
关于这一点可以在 Python 中找到
C API 文档
www.python.org 网站。

在托管中嵌入 Python 时
应用程序,你必须管理
GIL 就像你一样
将 Python 嵌入到 C 或 C++ 中时
应用程序。

在与任何人互动之前
提供的对象或 API
Python.Runtime命名空间,调用代码
必须已经获得Python全局
解释器锁通过调用
PythonEngine.AcquireLock 方法。 这
该规则的唯一例外是
PythonEngine.Initialize 方法,其中
可以在启动时调用,无需
已获得 GIL。

使用完 Python API 后,
托管代码必须调用相应的
PythonEngine.ReleaseLock 释放
GIL 并允许其他线程使用
Python。

AcquireLockReleaseLock
方法是薄包装
非托管 PyGILState_Ensure
PyGILState_Release 函数
Python API 和文档
这些 API 适用于托管
版本。

另一个问题是 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:

Important Note for embedders: Python
is not free-threaded and uses a global
interpreter lock to allow
multi-threaded applications to
interact safely with the Python
interpreter. Much more information
about this is available in the Python
C API documentation on the
www.python.org Website.

When embedding Python in a managed
application, you have to manage the
GIL in just the same way you would
when embedding Python in a C or C++
application.

Before interacting with any of the
objects or APIs provided by the
Python.Runtime namespace, calling code
must have acquired the Python global
interpreter lock by calling the
PythonEngine.AcquireLock method. The
only exception to this rule is the
PythonEngine.Initialize method, which
may be called at startup without
having acquired the GIL.

When finished using Python APIs,
managed code must call a corresponding
PythonEngine.ReleaseLock to release
the GIL and allow other threads to use
Python.

The AcquireLock and ReleaseLock
methods are thin wrappers over the
unmanaged PyGILState_Ensure and
PyGILState_Release functions from the
Python API, and the documentation for
those APIs applies to the managed
versions.

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.

倒数 2024-08-05 01:38:18

大多数依赖 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.

猥琐帝 2024-08-05 01:38:18

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).

怪我闹别瞎闹 2024-08-05 01:38:18

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.

静谧幽蓝 2024-08-05 01:38:18

至于 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.

煮茶煮酒煮时光 2024-08-05 01:38:18

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."

夜唯美灬不弃 2024-08-05 01:38:18
  1. Ironpython 与 C# 类似,它依赖于静态预构建库,而与 C# 不同,它是一种动态语言。

    Ironpython 与 C# 类似,又依赖于静态预构建库,而与 C# 不同,它是

  2. Cpython 就像 C++ 一样,Ironpython 是一种动态语言,可以访问动态库,这反过来又意味着被迫编写所有内容。

    Cpython 就像 C++ 一样,

  3. Ironpython 在某些方面比 C# 更快,但不比 Cpython 快,但是您可以将 Ironpython 链接到任何语言,从而克服即将出现的问题,但您也可以对 Cpython 做同样的事情。

无论您选择什么,都是一种有趣、简单且功能强大的语言!

  1. Ironpython is like C# in turn it relies on static prebuilt libraries while unlike C# is a dynamic language.

  2. 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.

  3. 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!

沧笙踏歌 2024-08-05 01:38:18

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.

じ违心 2024-08-05 01:38:18

我主要更喜欢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文