Unfortunately, I haven’t been able to reproduce the issue with a minimal working example.
I was hoping there might be a way to debug the crash or at least understand what triggers it, as it occurs only during the reload process.
The key observation is that everything works correctly on the initial load. The crash happens only when reloading, after using the extension and making any change to the code.
My next step will be to duplicate the extension and reintroduce files one by one to see whether a specific module or script is causing the problem.
By the way, I am quite surprised by how good Co-Pilot is for PyRevit:
There isn’t a single universal cause for “pyRevit works on first load but crashes on reload”, but this pattern is very well‑known in the pyRevit/RevitPythonShell ecosystem. The behaviour almost always points to one of a few underlying mechanisms.
Let me walk you through the most common root causes and how to diagnose them. You’ll probably recognise one of these from your extension.
Why pyRevit crashes only on reload
1. Module caching + circular imports
On first load, Python imports everything cleanly.
On reload, pyRevit tries to unload modules, but circular imports or modules stored in global variables remain in memory.
This leads to partially‑initialised modules on reload → crash.
Typical signs
-
import moduleA inside moduleA itself (direct or indirect)
-
utilities importing UI scripts and vice‑versa
-
abstract classes importing concrete implementations
Fix
-
Break cycles
-
Move shared utilities into a clean, dependency‑free module
-
Ensure UI scripts never import heavy modules unnecessarily
2. Persistent .NET objects not being released
If your extension holds references to:
-
Revit API objects
-
UI elements
-
Event handlers
-
External events
-
IDisposables
…they survive the first run and cause a crash on reload.
Fix
-
Ensure every event handler is unsubscribed
-
Avoid storing Revit API objects globally
-
Dispose of anything disposable
-
Never keep references to UIApplication, UIDocument, or elements outside the script scope
3. Leftover compiled Python files (.pyc)
pyRevit sometimes reloads the wrong cached version.
Fix
4. Namespace collisions
If two modules share the same name (e.g., utils.py in multiple extensions), the first load works, but reload picks up the wrong module.
Fix
5. Reloading abstract class hierarchies
Given your interest in multi‑level abstract classes:
If an abstract base class is imported before its subclass, and pyRevit reloads them in the wrong order, Python sees a “new” version of the class and mismatched inheritance → crash.
Fix
-
Ensure imports are strictly top‑down
-
Avoid dynamic imports inside class definitions
-
Keep ABCs in a stable, dependency‑free module
6. pyRevit’s own reload mechanism limitations
pyRevit does not fully replicate Python’s importlib.reload.
Some modules simply cannot be safely reloaded if they hold:
-
static state
-
class‑level caches
-
global singletons
-
monkey‑patched functions
Fix
-
Move stateful logic into functions
-
Avoid module‑level execution
-
Use lazy imports where possible
The debugging strategy you mentioned is the right one
Duplicating the extension and adding files one by one is exactly how I would approach it.
But you can speed it up with this checklist:
Quick elimination test
-
Comment out all imports in your main script
-
Reload → if no crash, the issue is in one of the imported modules
-
Re‑enable imports one by one
-
When the crash returns, you’ve found the culprit
If you want, I can help you pinpoint the exact cause
If you share:
…I can usually identify the root cause very quickly.