For the complete documentation index, see llms.txt. This page is also available as Markdown.

Migrating from IronPython2 to PythonNet3

Migrating from IronPython2 to PythonNet3

Explicitly calling the base class constructor

Now, in Python classes that inherit from a .NET type (like Winform, WPF, DataTable, or an Interface), if you overload the method _init_, you must explicitly call the base class constructor using super()._init_(...).

Example (WinForms):

class TestForm(Form):
    def __init__(self):
        super().__init__() # add this line
        self.Font  = System.Drawing.SystemFonts.DefaultFont
        self.InitializeComponent()
   
    def InitializeComponent(self):
        self._buttonCancel = System.Windows.Forms.Button()
        self._buttonOK = System.Windows.Forms.Button()
        self.SuspendLayout()

Specific Syntax of .NET Class Interface Implementation

Implementing .NET Class Interfaces, which was difficult under CPython3 (PythonNet 2.5), is now fixed and made easier with Dynamo_PythonNet3.

A Python class deriving from a .NET class must have the attribute namespace.

Example (Implementation of ISelectionFilter):

The UI with WPF

IronPython allows the direct use of WPF thanks to a specific library (wpf).

There is no similar library with PythonNet3. However, with some concessions on Binding, it is still possible to use WPF (even with the MVVM pattern) via XamlReader.Load(StringReader(xaml))

Here is an example of assigning a sub-project by Element type using the MVVM pattern:

MVVM

Respect method signatures

IronPython is more permissive. With PythonNet, you must strictly adhere to the signature of a .NET method, which means that:

  • The method name is correct.

  • The method is called on the correct type of object (instance method vs static method).

  • The types of objects passed as arguments are correct.

If you pass a native Python list to a .NET method that expects a .NET collection, you must explicitly cast/convert the Python list.

Example: Casting a Python list to List<\ElementId> :

Specific syntax for out and ref parameters

With PythonNet3, the parameters out or ref appear as normal arguments in Python, but the return value of the method is modified: The method returns the modified value(s) of these parameters as a tuple.

Example (Using ComputeClosestPoints with a parameter out):

IronPython (old syntax):

Please note that there is a difference with the old version “PythonNet2.5” when the method returns nothing (Void)

Access to CPython libraries coded in C

PythonNet3 provides excellent access to the entire modern PyPI ecosystem, including libraries that use C extensions (such as numpy, pandas, scipy, openpyxl). IronPython has an incompatibility with these libraries.

LINQ

Just as IronPython PythonNet3 supports LINQ method extensions, it will nevertheless be necessary to specify the Types of the objects passed as arguments. See examples in the next section.

No direct access to COM objects

PythonNet does not implement DLR, unlike IronPython. As a result, dynamic access directly to COM object properties is not possible.

Two workarounds:

  • Use .NET Reflection

  • Cast objects to the correct interface Example of using .NET Reflection:

Last updated