Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug support #2518

Open
matyascsanady opened this issue Jan 14, 2025 · 6 comments
Open

Debug support #2518

matyascsanady opened this issue Jan 14, 2025 · 6 comments
Labels
New Feature New feature request [class->Implemented #{number}: {title}]

Comments

@matyascsanady
Copy link

As someone who regularly develops pyRevit extensions, I believe that adding debugger support would be a significant quality-of-life improvement for developers. Debugging pyRevit extensions can be challenging without a proper tool, and I’ve noticed that this topic has been previously discussed on the pyRevit forum (e.g., this thread on attaching the VS Code debugger).

From this forum post, it seems there was an interest in integrating debugpy to enable attaching the VS Code debugger. I’ve tried to implement this in my own extension, but could not make progress with it.

I’m curious if this feature is still on your roadmap. I understand that the team’s focus is likely on Revit 2025 compatibility, but implementing proper debugger support would greatly enhance the developer experience for the pyRevit community.

If there are any updates or guidance on how to integrate debugging capabilities in the meantime, I’d love to hear about them. Thank you for all your efforts in building and maintaining pyRevit!

@matyascsanady matyascsanady added the New Feature New feature request [class->Implemented #{number}: {title}] label Jan 14, 2025
@sanzoghenzo
Copy link
Contributor

Hi @matyascsanady, I don't think there's a roadmap at all 😅

@eirannejad isn't active on the project anymore, and I don't see traces of debugpy in the code base, so it must have been something he tried locally but never released.

And unfortunately IronPython seems to be unsupported by debugpy: microsoft/debugpy#523 (comment)

We may have some chances with CPython/pythonnet, but at this time it doesn't support WPF/pyrevit.forms; if you don't use that, you can try to run your script using the #!python3 shebang at the beginning of the file and see if it works.

There's also a debugger feature inside pyrevit that uses pdb (so you have to learn some commands to use that), but I found an issue a while ago and never managed to investigate it further (despite my claims in the last comment of that issue 😅 ).

@matyascsanady
Copy link
Author

Hi @sanzoghenzo! Thank you for your quick response!

I saw that IronPython does not support debugpy, so I’ve been trying with CPython. My issue seems to be with debugpy.listen(), where no matter what port I give, it raises a runtime error instantly stating that "timed out waiting for adapter to connect", but weirdly enough it starts a brand new instance of revit. 😅 (i know that this specific issue it not pyrevit related, just thought i'd share it if you guys ever plan to implement it)

I also noticed that pyRevit implements a debugger (pdb), but to be truthful, I wasn’t really satisfied with that approach. Proper integration with debugpy and VS Code would be a game-changer.

@sanzoghenzo
Copy link
Contributor

sanzoghenzo commented Jan 14, 2025

I feel you, now that we are spoiled with graphical debuggers, why resorting to the hardcore, ugly, old tools? 🤣

Can you share what have you done so far here, or even better in the forum discussion? That way we might re-ignite the discussion on that topic, knowing what has been done and what didn't work.

@matyascsanady
Copy link
Author

Okay so i got it working @sanzoghenzo

  1. You have to install debugpy for python 3.7.8 (you can do this by installing the interpereter than path_to_interpreter/python -m pip install debugpy, you may need install a specific version (1.5.1) of debugpy by running the before script like this path_to_interpreter/python -m pip install debugpy==1.5.1)

  2. You have to configure your launch.json in VSC

        {
            "name": "Attach to Revit",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "",
                "port": 5678
            }

Where host by default is 127.0.0.1

  1. You have to configure and create the debugpy instance (no idea of the terminology) in your pyRevit extension. For example you can do it in a startup.py script or in a dedicated button or whatever. Important this file has to be run with CPython (#!python3). An example for setting up the debugger
#!python3
import os
import sys

sys.path.append(r'%userprofile%\AppData\Local\Programs\Python\Python37\Lib\site-packages')

if not os.getenv("PYREVIT_DEBUG"):
    os.environ["PYREVIT_DEBUG"] = "1"

    import debugpy

    debugpy.configure(python="C:\\pyRevit\\main\\bin\\engines\\CPY378\\python.exe")
    debugpy.listen(5678)
    debugpy.wait_for_client()
    print("Debugger attached!")

else:
    print("it dead")

The whole if statement is required otherwise it would start a new revit instance (dont ask me why...). debug.configure() is extremly important as debugpy has to be configured with the correct interpreter (this debugy issue) @sanzoghenzo this was the timeout issue i mentioned earlier
You can also listen on a specified address instead of the default local one debugpy.listen(("127.0.0.1, 5678"))
You can also specify a bigger timeout treshold for the wait_for_client() by passing an argument to it debugpy.wait_for_client(timeout=60)

  1. If you have set it up correctly after the runing the script above as part of your pyRevit extension, you can attach the debugger to it VSC.

  2. It's very important that no regular breakpoints will be hit (placed in VSC) you have to use the debugpy.breakpoint()

  3. If a debugpy breakpoint is hit (or an exception is thrown) you will have the ability to debug line by line is VSC

It is by no means perfect. As mentioned no WPF support (even if you import pyrevit.forms it dies).
Furthermore simple stuff like

a = 2
b = 3
c = a + b

is deconstructed in the VSC debugger like so

a = 2
b = 3
LOAD_NAME(a), LOAD_NAME(b), BINARY_ADD, STORE_NAME(c)

I did not have the time to test it properly just wanted to share my work in progress solution.

@sanzoghenzo
Copy link
Contributor

That's good to read @matyascsanady !

One thing I should have mentioned, you could try with the WIP version that ships an updated version of pythonnet (version 3) and CPython 3.12

Also something that could help: maybe with this configuration VSC breakpoints will work
https://stackoverflow.com/a/78047184/4149512

@matyascsanady
Copy link
Author

Sorry for my absence @sanzoghenzo!

I'll take a look at the WIP version (with the CPython 3.12 there is definitely no need to install an older version (1.5.1) of debugpy as i've mentioned earlier) and the stackoverflow post you sent, when I get the chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New Feature New feature request [class->Implemented #{number}: {title}]
Projects
None yet
Development

No branches or pull requests

2 participants