https://www.alternetsoft.com.au/blog/python-net-iron-python-scripting

Python and IronPython scripting and debugging

Learn how to seamlessly integrate Python and IronPython scripting, along with debugging, into .NET applications.
25 Jan 2022 4 min read

With the latest release of AlterNET Studio 8, we support yet another scripting and debugging engine based on Python.NET.

This new engine allows you to seamlessly integrate Python scripting and debugging capabilities into your .NET applications. It’s very similar in terms of usage to IronPython scripting engine; however, you need to consider some differences to choose one over another.

IronPython scripting

Initially developed by Microsoft, IronPython offers a robust Python execution environment within the .NET Framework. It provides two modes of operation:

  • Interpreted Mode: This mode is used by the script debugger for interactive execution and debugging.
  • Dynamic Assembly Generation: IronPython can also generate dynamic .NET assemblies, providing performance benefits for compiled code.

IronPython is fully integrated with .NET; it has a very light footprint and doesn’t require a separate Python installation on the target machine. Additionally, the standard library modules (like datetime, math, etc.) are included within the IronPython.Modules.dll assembly, eliminating the need for external Python files.

IronPython Scripting

While IronPython offers many advantages, it’s important to note some performance considerations:

  • IL Code Generation and JIT-ing: Due to its .NET integration, IronPython scripts require compilation into Intermediate Language (IL) code and subsequent Just-In-Time (JIT) compilation into machine instructions. This can lead to a noticeable startup delay, especially for large scripts.

  • Third-Party Library Limitations: IronPython does not support C-like Cython code, which is used by some popular third-party Python libraries. This limits its ability to leverage these libraries within IronPython scripts.

Python.NET scripting

Strengths of Python.NET Unlike IronPython, Python.NET avoids the limitations of IL code generation and supports more recent versions of Python. It allows seamless integration with popular libraries like NumPy and Pandas, expanding your Python scripting capabilities. Python.NET provides smooth integration with .NET assemblies, enabling you to leverage application-defined .NET objects within your Python scripts.

The downside: Global Interpreter Lock (GIL) Unlike IronPython’s multithreading support, Python.NET scripts are subject to the Global Interpreter Lock (GIL). This means you cannot truly execute multiple Python.NET scripts concurrently in separate threads

Python installation requirement

Another consideration is that Python.NET requires either the full Python installation or an embedded Python distribution to be present on the target machine. We use the embedded Python distribution in our examples which does not require the user to install full Python to see how it works.

Python and IronPython script debugging

Both Python.NET and IronPython scripting engines offer a nearly identical set of debugging capabilities. This includes step-by-step execution, breakpoints, evaluating watches and local variables, and call stack display.

Python and IronPython script debugging

Seamless Integration with .NET Applications

These debuggers seamlessly integrate into .NET applications due to their ability to execute interpreted code line by line. This eliminates the limitations encountered with C# and Visual Basic debuggers, providing a more fluid and efficient debugging experience.

Thread safety in script debugging

One known limitation with both Python.NET and IronPython scripting engines is that the script debugging session runs in a separate thread. This necessitates thread-safe access to application-defined objects, especially when dealing with UI controls. To ensure correct behavior, scripts must use the Invoke method to access properties of UI controls in a thread-safe manner.