pyRevit and Revit 2026 - reload problems

Hi,

My code is working fine.

I can amend and reload it just fine. But if I reload after any use of the extension, Revit crashes, regardless if I have updated the code or not.

It wasn’t occur at the very early stage when I was testing some examples. This should be something linked to my code but not idea why it behaves in this way. Practically, I am developing my code and after each test, I need to close Revit and re-opening.

Any idea? thanks

Without seeing the code itself… Impossible to have an idea…

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.


:magnifying_glass_tilted_left: 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

  • Delete all __pycache__ folders inside your extension

  • Restart pyRevit

  • Try again


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

  • Use unique module names

  • Or package your extension as a proper Python package with __init__.py


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


:test_tube: 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

  1. Comment out all imports in your main script

  2. Reload → if no crash, the issue is in one of the imported modules

  3. Re‑enable imports one by one

  4. When the crash returns, you’ve found the culprit


If you want, I can help you pinpoint the exact cause

If you share:

  • your folder structure

  • the import graph

  • the modules involved

  • the error message (if any)

…I can usually identify the root cause very quickly.

I would not say copilot is good. And it is definitely verbose.
Nothing here is really specific to pyRevit. Those are very generic debugging steps.