Use Autodesk Imports with pyRevit Transaction

I got my head around python syntax, logic of scripts and how to implement it within pyRevit. However, I do not fully understand what’s happening behind the scenes when it comes to python and pyRevit.


  1. As far as I understood from reading the topics here in pyRevit forum, when importing, let’s say, classes from “Autodesk.Revit.DB”, the script runs faster as opposed to “from pyrevit import DB”. However, when it comes to transactions, I got an impression that to use “with revit.Transaction” is better than try-except-finally. Is this okay to use imports “from Autodesk.Revit.DB import something” with revit.Transaction as opposed to from “pyrevit import DB” ? Please see example below:
from Autodesk.Revit.DB import (FilteredElementCollector, View) # faster
from pyrevit import revit 
- - - - - - - 
with revit.Transaction...# better handles transactions

(I’m more leaning to the top option, but is it okay from pyRevit perspective?)


from pyrevit import DB, revit # slower
with revit.Transaction...# better handles transactions
  1. Does pyRevit transaction context manager takes care of the Dispose() part?

  2. When it comes to C#, they talk about Garbage Collector (GC). If I use Autodesk.Revit.DB imports (not from pyrevit import DB) and pyRevit transaction (with revit.Transaction), does pyRevit takes care of the GC?

Why should it? pyrevit.DB is in fact Autodesk.Revit.DB already imported for you without the need to handling clr yourself.


Pyrevit uses ironpython or pythonnet libraries to run python scripts; they handle the Garbage collection (by using the .net gc, if I understood correctly).

1 Like

Also, do you know anything about this bit?

I just use “from Autodesk.Revit.DB import something” without adding CLR references. Looks like pyRevit environment handles it automatically?

No need to worry about dispose for transactions.

The “with” block should handle this automatically

1 Like

Yes, in the pyrevit.api module, in which you can see that it also does from Autodesk.Revit import DB; that same DB is then exposed in the pyrevit init module, so it is exactly the same thing.

The code for pyrevit’s Transaction class can be found here.
the __exit__ method is the one that is called when exiting from the context manager, and you can see that it handles the Commit if everything is good and Rollback in case of an exception, but there’s no call to Dispose.

I’m not an IronPython guru, so I don’t know if the Dispose gets magically called during the garbage collection, or if it is an overlook by our part and we should call that method on exit.

The only piece of information that I can find is that with IronPython you can use the with statement on IDisposable objects to automatically dispose them on exit (but it’s not happening here).