Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace IronPython with Python.Net?

Tags:

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?

like image 661
Andrew Stephens Avatar asked Dec 03 '20 10:12

Andrew Stephens


1 Answers

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
like image 131
LOST Avatar answered Sep 22 '22 02:09

LOST