pyRevit 5.1 No module named Windows

This is the beginning of my the code I am testing it with pyRevit 5.1 and Revit 2025 and 2026:

import os
import clr
import json

from System.Windows import Window

I get the following error:

line 5, in <module>
ImportError: No module named Windows

Does that mean we can’t use wpf with pyRevit anymore?

you need to explicitly import the required modules in net core (revit 2025 2026)

import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
clr.AddReference("WindowsBase")

from System.Windows import Window
1 Like

Thx, Jean!

seems like we bypassed the problem, but now we hit the IronPython loading issue, here’s the new code snippet:

import os
import clr
import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
clr.AddReference("WindowsBase")

from System.Windows import Window

import Autodesk.Revit.DB as DB
from Autodesk.Revit.DB import *
import Autodesk.Revit.UI as UI

try:
    clr.AddReference("Ironpython.Wpf")
    import wpf
except:
    clr.AddReferenceToFileAndPath(r'C:\Program Files\IronPython 2.7\DLLs\IronPython.Wpf.dll')
    import wpf

and the error:

**IronPython Traceback:**
Traceback (most recent call last):
File "B:\06. BIM AUTOMATION\06. IPA\IPA.extension\IPA.tab\Water Supply & Sewage.panel\Dipe.pushbutton\script.py", line 18, in <module>
IOError: [Errno 2] Could not load file or assembly 'IronPython, Version=2.7.12.0, Culture=neutral, PublicKeyToken=7f709c5b713576e1'. The system cannot find the file specified.

This is valid for both Revit 25 and 26. However, there already is a topic for that in the repo, so I will continue there :slight_smile:

There’s also a new issue in earlier Revit versions though (just tested it in both Revit 24 and 21)

**IronPython Traceback:**
Traceback (most recent call last):
File "B:\06. BIM AUTOMATION\06. IPA\IPA.extension\IPA.tab\Water Supply & Sewage.panel\Dipe.pushbutton\script.py", line 19, in <module>
File "wpf.py", line 14, in <module>
ImportError: No module named _wpf

or should I maybe raise this as a separate topic?

Pyrevit ships with ironpython preloaded. No need to import it
Make q search in the code base in GitHub to see how it is done.
Plenty of examples of wpf usage in the code base

1 Like

I’ve actually been using the same code with pyRevit versions up to 4.8 and Revit 21 to 24 and it has always worked fine. I’ve had some issues with IronPython loading, which is why at some point I added the explicit reference. I only encountered this issue with pyRevit 5.1

I did a lot of digging, though, and I actually found out what was the cause and the solution, so posting it here for anyone that might be looking for some proper information and support:

  • in the new .NET Core-based environment IronPython wpf can’t resolve dynamically from arbitrary paths like it used to
  • even if the DLL is there, it might require a very specific version, like IronPython 2.7.12, while having 2.7.11 installed

The best solution seems to be migrating from IronPython WPF and switching to pyrevit.forms.WPFWindow instead, making it fully compatible with pyRevit 5.1 and safer to use with Revit 25, 26 and future versions, while also keeping its compatibility with older Revit versions. This worked for me and already tested it successfully with Revit 21, 23, 25 and 26. I hope that helps someone having a similar issue :pray: