Obstacles encountered with PythonNet3 and workarounds
Properties of type “Indexer”
Properties of type “Indexer” (like access to Space in the Revit API) are no longer handled directly with the bracket syntax. The indexer must be replaced by the corresponding get method.
Example:
Old code:
space = elem.Space[phase]New code:
space = elem.get_Space(phase)Error in Python set collection
The Python Collection set() does not accept objects of type ElementId.InvalidId.
Workarounds:
Filter invalid IDs when building the set()
Use the .NET class _HashSet<T>
Example(Filtering):
setViewsIds = set([w.OwnerViewId for w in allWires if w.OwnerViewId != ElementId.InvalidElementId])"with" statement on .NET objects
The use of the keyword with on .NET objects (which implement the interface IDisposable) currently throws an error with PythonNet3, unlike IronPython.
Workaround: Build a custom context manager to explicitly handle the method Dispose().
Example (Custom Context Manager CManager):
.NET Objects that Inherit from an Interface
In the case of objects that inherit from a .NET Interface, it may be necessary to cast (convert) the object to that interface to use the inherited methods.
Example (Call BeginInit() on a PictureBox):
In the example below, you will find the PythonNet syntax to perform an explicit cast to a .NET interface.
The line _System.ComponentModel.ISupportInitialize(self.pictureBox1).BeginInit() is therefore a method call on an explicit interface:
It “casts” (or rather, it accesses) the object _self.pictureBox1 via its implementation of the interface ISupportInitialize.
It then calls the method BeginInit() of this interface
Attributes lost on .NET Class Instances when assigning to OUT
Python class instances deriving from a .NET class may have their attributes removed by the Dynamo Wrapper when assigning to the variable OUT.
Workaround:
Wrap the .NET object in a simple Python class before assigning it to OUT.
Example:


Missing error line
In rare cases, the error line number in the Python node error message may be missing.
Here are some solutions while waiting for a fix:
Implement a try-except block with the traceback library and print the error. Implement a try-except block with a logger and write the error. Implement a debugger, for example, using sys.settrace().
Conclusion PythonNet3 establishes itself not only as the current standard but as the only sustainable path forward for development in Dynamo.
Last updated