Import libraries inside pyRevit environment

Can anyone please explain what is the right way to start working in pyRevit environment. I mean what “must have” libraries I need to import to start working?

I tried to study various scripts and everyone uses different methods.

  1. Some people use clr. Do I need to import this when working in pyrevit?
    import clr
    clr.AddReference(“RevitAPI”)

  2. Some people do this which does not make sense.
    from Autodesk.Revit.DB import Workset, Transaction
    (this line makes sense as a person tries to create worksets and record transaction so it can be undone).
    from Autodesk.Revit.DB import *
    (then they do this which does not make sense. They have already imported what they need and now they import everything?).

  3. If I import the above do I need to import this:
    from pyrevit import revit, DB

  4. Also, when it comes to Transaction:
    Is that okay if I use standard RevitAPI transaction with start() and commit()?
    I noticed that some users simply use with revit.Transaction().

  5. Let’s say, I don’t import something that I need for my script from Revit API, will pyRevit give a warning / error to stop a script? Or there is a chance that Revit model gets corrupted?

Please advise.

Welcome @VitalijM,

no use if you import pyrevit modules

basically you should either import the Revit API from Autodesk.Revit or from pyrevit module, but not both.

The pyRevit way would be:

from pyrevit import DB, revit
# then
DB.Transaction
DB.Workset 
# to call on Revit API DB objects

No, you don’t as long as you don’t use any pyrevit specific methods in the revit and DB modules

Yes, it can come in handy to use it the regular way, but the pyRevit way handles the transaction more nicely and with added features when using:

with revit.Transaction('Name of the Transaction"):
    # do stuff

https://ein.sh/pyRevit/reference/pyrevit/revit/db/transaction/#pyrevit.revit.db.transaction.carryout

Yes, you will get the traceback in an output window like this one for example

Thank you for the reply.

Last question: when I import the below, does it import everything? Or when I call on Revit API objects, let’s say DB.Workset, it only imports whatever I need (Workset Class in this instance)?
The reason I’m asking is that some say that we need to try to avoid importing everything (import * ).

from pyrevit import DB, revit

I think in this case, it imports everything, same as import *
DB being a wrapper around the list of RevitAPI DB

Interesting questions there.

Why you shouldn’t use import * has to do with a thing called scope.
I’m not going to go too deep into what that means so here’s an analogy.

Imagine you’re working on a lego project, and you have a box that stores all your bricks. (DB)
You are working very clean, and have sorted out all the bricks with different colors and shapes into different boxes inside your bricks box.

If you use a import like so:
from Autodesk.Revit import DB
You’re telling python to look inside the bricks box and get whatever you need.

If you use an import like so:
from Autodesk.Revit.DB import *
It’s more like this
image

Thank you. Explaining things as if you are talking to a 5-year old are the best. No sarcasm here.