Replace IronPython with Python.Net?
Asked Answered
C

1

7

We've been using IronPython for a number of years as a scripting tool within some of our .Net desktop applications. Customers have been asking when we will support Python 3, but it looks like IronPython3 is still a long way off (https://github.com/IronLanguages/ironpython3). I've just found a library called Python.Net, and was wondering how similar this is to IronPython, and whether it is something we could move over to?

Most of the Python.Net examples show how to use .Net types from within Python code, and I can't find any good examples of how you actually run Python code from within C#. There's a section headed "Embedding Python" near the end of the page (http://pythonnet.github.io/), but it doesn't go into much detail.

I'm interested to know how to run a script from either a file or a string, whether scripts can be "compiled" (like IronPython), presumably "variables" can be passed back and forth like the IronPython scope? Are there any restrictions or known issues? Does Python.Net allows running of scripts that reference libraries such as numpy? (We were unable get this working with IronPython, which I believe was due to it containing compiled (.pyc) code, which IP doesn't support).

Does anyone have experience of both, and able to offer some insight on how they compare?

Caines answered 3/12, 2020 at 10:28 Comment(2)
Hi @Andrew Stephens, just wonder what you ended up doing. IronPython released 3.4. Do you think it's a good idea to use it? Do you think it's a good choice for a situation for relative simple embedded functions that that do not need to have access to many external Python modules?Parochial
We've stuck with IronPython and Python 2.7 for now, as IronPython 3 is still(!) in beta. IP is still very capable though, and we do some complex stuff with it. It does support importing external modules, just not things like numpy (.pyc). I have been investigating Python.Net recently, and this looks promising although it would be a lot of refactoring for us to switch. If you do take a look, stick with the latest (currently v3) as I found earlier versions to be very basic with no way to pass values between Python & C# (unless I was missing something).Caines
L
8

A short summary: originally Python.NET was designed to be a drop-in replacement for IronPython, so most of what you want is possible with the major caveat: you need to also have a regular Python interpreter.

Now to more details:

  1. Running Python script - PythonEngine class has Exec and Eval functions.
  2. "compiled" - Python.NET has no option to compile scripts. You must pass them as strings and/or files to PythonEngine (which is just a representation of Python interpreter).
  3. like the IronPython scope - there is a PyScope class, which lets you control a group of Python variables.
  4. restrictions or known issues - there are quite a bit. You can't access all CPython APIs, and some .NET APIs might be impossible to call from Python, but for most part it will just work.
  5. allow .. numpy - Python.NET can call any Python library. In fact, I use it extensively to wrap TensorFlow for C# (which also includes NumPy).

The issues Python.NET has compared to IronPython:

  • requires Python interpreter to be installed and managed separately
  • function overloading is not as good
Loewe answered 3/12, 2020 at 18:51 Comment(6)
Thanks for the comprehensive answer. Any comments/thoughts on performance? If a script can't be "precompiled" as you can in IronPython, does this mean performance isn't as good?Caines
If the script is going to contain a lot of .NET calls, it might suffer performance hit. However, it will depend on the usage patterns. I expect numpy to be fast. TensorFlow certainly is. If anything speaks to it, there's a quantitative trading company using Python.NET for Python scripting: quantconnect.comLoewe
@Loewe what process executes the python script in python.net? does a python process get called to execute this script?Antonietta
@Antonietta Python code executes in the same process using docs.python.org/3/extending/embedding.htmlLoewe
@Loewe so am I right in saying that C# is calling the python interpreter, so C# is the process that is executing the scripts through the GIL?Antonietta
@Antonietta sounds correct.Loewe

© 2022 - 2024 — McMap. All rights reserved.