Compiling Error

Hello all, I have a script that when running from CLI crashes moments after it starts to run the script. I have attempted to save some logs to a text file but nothing is saved and no warning is given. The first write to txt is on line 3.

First 3 lins:

import os, sys
with open ("C:\\users\\shansen\\Desktop\\File.txt", 'w')as f:

Since it was crashing with no errors I started commenting out large chunks of the script from the bottom up. I finally got to the point where only definitions and imports were left. I commented out one of the definitions and all of my writes started working. At this point, the definition is not being called anywhere in the script but still causing it to crash leading me to think it’s a compiling error.

The definition is just under 700 lines long. I continued with the above method of commenting out lines from the bottom up. Eventually, the script started working again. This is where things start to get strange. Once I comment out line 426 (of the definition, not the overall script, and assuming the definition starts at line 1 and has no blanks or commented lies) it starts working.

Line 425 and 426:

text = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.TextNoteType).ToElements()
textCount = len(text)

Nothing on these two lines looks incorrect to me. I then added “hi = 1” before textCount and commented out textCount and the script crashes. Anything I add past line 425 causes the script to crash. I can add comments and blank lines before or after to increase the line count but any code past line 425 crases it.


text = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.TextNoteType).ToElements()
#textCount = len(text)
hi = 1


#text = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.TextNoteType).ToElements()
#textCount = len(text)
hi = 1
by = 2

works fine

text = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.TextNoteType).ToElements()
#textCount = len(text)
#hi = 1
#by = 2

If I open Revit and run the tool from the ribbon it executes the script as expected with no warnings.

Is there a way I can get an error report when running from the CLI? Since it is crashing during the compiling phase I am not able to write anything to a text file.


I am running the definition inside a single for loop. I just copied the whole definition and passed the code into the loop. It executes no problem. I am not sure why it would cause the compiler to crash when in a definition vs directly in the loop.

If you have any ideas please let me know.

Thank you,

Hmm that does seem very odd indeed. I’ve had some run-ins with some unexpected crashes before.
I can’t give you an answer to your problem, but maybe I can point you into the direction of a possible solution. You say it’s a compiling error, but then I’d expect you could provide us with an exception message. Since you don’t have it, I thiiiink this is not a compilation error. (Python isn’t really a compiled language) and if your revit’s python shell can interpret the script, there should be no reason why the cli cannot.

First off, for clarity, I’ll be calling definitions “fuctions” or “methods”, that’s the more generic name of definitions. You’ll probably find way more results if you use the word function or method instead of definition. Just a tip.

As for the real bug hunting.

What strikes me as odd is that your script’s crash seems most definitly related to that part of the ElementCollector. More specifically, you’re interacting with the Revit Document. (You’re using it to create a collector).

I’ve run into revit crashes before when I’m interacting with the Document, most often this is when my script is NOT run by revit, but run from outside of Revit. Revit is a single-thread program and it does NOT like interferance from processes on the outside. Maybe the CLI is accessing revit from the outside at a time that the document is being used by revit itself?

What I recommend trying is checking your revit journals, they can sometimes contain a clue about what happened right before the crash. My suggestion when looking at journal logs?
Don’t get overwhelmed by the info, but keep your eye open for things that seem out of order. (This is tough at first, but you’ll get better at it eventually)
I think revit appends journals from the bottom, so scroll all the way down first.

You can also try adding a try/except block around the part that causes the crash. Something liek this

    text = FilteredElementCollector(doc).OfClass(Autodesk.Revit.DB.TextNoteType).ToElements()
except Exception as e:

It probably won’t stop the error from stopping your script, but it MIGHT prevent it from travelling further upwards and crashing your revit before it can do something.

A final thing I want to add, and this is a suggesiton, but one I really recommend is:
Split up your functions.
700 lines in a single definition is a lot. Like reaally a lot.
You can keep your code more organised by splitting the thing up into different functions.
Let that script be by itself for 1 week, get back to it, and you’ll be pulling out your hairs out of frustration. You’ll be thinking “what the hell is this mess”.

The term I’m referring to is “recfactoring”, it’s a fancy way of saying tidy up your code.
There’s some great videos on youtube that can teach you. I’d recommend something from “Uncle Bob”