[Solved]:How to debug workbench with vscode, and macro debug

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

[Solved]:How to debug workbench with vscode, and macro debug

Post by xianyu »

Hi,I have some problems debugging python with vscode.
I want to debug some python code of my workbench.When I debug directly, I get an error. Obviously FreeCAD is not a python package
1.png
1.png (1.38 KiB) Viewed 2024 times
So, I went to the wiki for help and I followed this page
Debugging
https://wiki.freecadweb.org/Debugging#Python_Debugging
https://code.visualstudio.com/docs/pyth ... igurations
2.png
2.png (8.59 KiB) Viewed 2024 times
3.png
3.png (29.28 KiB) Viewed 2024 times
I must first click "Run Python File" and then click "Python :Attacher" debug button,If I click directly on the "Python :Attacher" debug button I get an error
4.png
4.png (4.07 KiB) Viewed 1851 times
I try to debug " FreeCAD.exe" using "Attach using Process ID".But the left variable in VScode is nothing ,and "step over" "step into"
"step out" button doesn't work.
5.png
5.png (14.91 KiB) Viewed 1757 times
When I open FreeCAD.exe ,it pops up a dialog reminding me of this

Code: Select all

unrecognised option '--for-server'
Allowed options:
Generic options:
-v[--ver3ion】Prints version string
.......
I looked up the post on debugging but couldn't solve my problem.I found that most of the posts were about debugging FreeCAD macros, and very little about debugging on Windows, and my job was not to macros. I need to debug python code in my workbench to verify that the workbench's commands work.


Currently,I'm doing workbench development on wiki.freecadweb.org/Download, the full installer.I don't know if this is the correct way to develop.
I have compiled "FreeCAD.sln "(This should not be what I want, I don't need to change the FreeCAD source code).
There is also a debug version of "FreeCAD_d.exe".
Or maybe I should use VS Community instead of VS code.
What should I do, could you help me?Thanks in advance.
https://forum.freecadweb.org/viewtopic.php?f=15&t=50917
https://forum.freecadweb.org/viewtopic. ... de#p468327
https://forum.freecadweb.org/viewtopic.php?f=10&t=35383
https://forum.freecadweb.org/viewtopic. ... de#p497581

OS: Windows 10 Version 2009
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24267 +148 (Git)
Build type: Release
Branch: Branch_0.19.4
Hash: 476ecf091941bead59b14e44afa6064d5a66afa3
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Last edited by xianyu on Thu Jul 14, 2022 5:50 am, edited 18 times in total.
Freecad novice, A Python enthusiast
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Question:How to debug workbench with vscode

Post by edwilliams16 »

See. https://wiki.freecadweb.org/Debugging#V ... VS_Code.29. You need to install ptvsd and copy it to FreeCads site-directory
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to debug workbench with vscode

Post by xianyu »

edwilliams16 wrote: Wed Jul 06, 2022 8:03 am See. https://wiki.freecadweb.org/Debugging#V ... VS_Code.29. You need to install ptvsd and copy it to FreeCads site-directory
Yes, I install ptvsd and copy it, too.But the same problem still exists :(

Code: Select all

xcopy  "D:\python3.8\Lib\site-packages\ptvsd"  "F:\installFC\FreeCAD 0.19\bin\Lib\site-packages\ptvsd"
Freecad novice, A Python enthusiast
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Question:How to debug workbench with vscode

Post by edwilliams16 »

Did you set the VSCode python interpreter in the status bar to be Freecad’s python?
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Question:How to debug workbench with vscode

Post by edwilliams16 »

I've only tried to debug Macros. I can imagine reloading issues with Workbenches if you edit files. I found the following useful when I was dealing with ptvsd issues:

Code: Select all

debug = True
if debug:
    try:
        #import sys
        #sys.path.append('/usr/local/lib/python3.9/site-packages')
        import ptvsd
        print("Waiting for debugger attach")
        # 5678 is the default attach port in the VS Code debug configurations
        ptvsd.enable_attach(address=('localhost', 5678))
        ptvsd.wait_for_attach()
    except ImportError:
        import sys
        print(f'Python Path {sys.path}')
        modulenames = set(sys.modules) & set(globals())
        allmodules = [sys.modules[name] for name in modulenames]
        modulenames = sorted([mod.__name__ for mod in allmodules])
        print(f'Loaded modules: {modulenames}')

print('Entered Macro')
which diagnoses what modules are loaded and where python is looking for them.
It was also tiresome to have to keep reinstalling ptvsd every time I get a new FreeCAD version, so I wrote the following, which can be modified for paths on non-Macs:

Code: Select all

#!/bin/bash
cd /Applications/FreeCAD.app/Contents/Resources/lib
ptvsd=$(find . -name ptvsd)
if test -z "$ptvsd"
then
    echo "Installing ptvsd"
    ../bin/python -m pip install ptvsd
else    
    echo "Has ptvsd already"
fi
which installs ptvsd into FreeCAD if it isn't already present.
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to debug workbench with vscode

Post by xianyu »

edwilliams16 wrote: Wed Jul 06, 2022 8:20 pm

Code: Select all

debug = True
if debug:
    try:
        #import sys
        #sys.path.append('/usr/local/lib/python3.9/site-packages')
        import ptvsd

Code: Select all

#!/bin/bash
cd /Applications/FreeCAD.app/Contents/Resources/lib
ptvsd=$(find . -name ptvsd)
if test -z "$ptvsd"
then
    echo "Installing ptvsd"
Thank you very much for your answer.
VSCode python3.8 was installed by me separately, and on a different system disk as FC.
I run the first piece of code and the Report view gives no errors, but when I click on my workbench, FreeCAD does not respond and crashes.The second piece of code is a bit hard for me, I didn't try it.
Freecad novice, A Python enthusiast
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: Question:How to debug workbench with vscode

Post by xianyu »

hi,guys
After continuous access to information and try, I finally solved this problem, about how to debug my own workbench in vscode.I hope it can bring some help to anyone developing their own workbench in the future. :)


My idea came from this post
https://forum.freecadweb.org/viewtopic. ... de#p536278

I will briefly summarize:
1)create a launch.json

Code: Select all

    "configurations": [
        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "connect": {
              "host": "localhost",
              "port": 5678
            },
            "justMyCode": false,
            "redirectOutput": true,
            "logToFile": true
          },
    ]
    
2)Add the following code at the beginning of the python code file.

Code: Select all

import debugpy
debugpy.configure(python="python")
debugpy.listen(5678)
debugpy.trace_this_thread(True)
debugpy.debug_this_thread()
Note that the debugpy library needs to be installed first

Code: Select all

python -m pip install --upgrade debugpy
# 5678 is the default attach port in the VS Code debug configurations.This corresponds to the default configuration of FreeCAD.
FreeCAD--Macro--Attach to remote debugger--VS Code
3)Open FreeCAD.
4)Click the start debugging button in vscode.
5)Enter code in FreeCAD's python console.

Code: Select all

FreeCADGui.runCommand('My_Command')
'My_Command' is the command name in your own python code
https://wiki.freecadweb.org/Command#Com ... _in_Python
6)Set breakpoints in python code, and when you operate in the FreeCAD interface, it will automatically jump to vscode breakpoints.


If you encounter this problem: No module named 'FreeCAD'
you can add this code

Code: Select all

FREECADPATH = 'F:\\installFC\\FreeCAD 0.19\\bin' 
import sys
sys.path.append(FREECADPATH)
import FreeCAD
Freecad novice, A Python enthusiast
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Solved]:How to debug workbench with vscode

Post by edwilliams16 »

Do you need a .env file to set paths? @xianyu or anything special in settings.json?
xianyu
Posts: 66
Joined: Mon Jun 27, 2022 7:34 am

Re: [Solved]:How to debug workbench with vscode

Post by xianyu »

I've tried many things with launch.json, but I'm sorry I haven't come across a situation where you need a . env file to set the path.do you have any problem?@edwilliams16
Freecad novice, A Python enthusiast
edwilliams16
Veteran
Posts: 3179
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Solved]:How to debug workbench with vscode

Post by edwilliams16 »

I had another try at using debugpy instead of ptvsd for debugging Macros in VSCode. I think I ended up with the same capability in the end.

My launch.json was

Code: Select all

{
    "configurations": [
    {
        "name": "Python: Attach debugpy",
        "type": "python",
        "request": "attach",
        "connect": {
          "host": "localhost",
          "port": 5678
        },
        "justMyCode": true,
        "redirectOutput": true,
        "logToFile": false
      },
]
}
and at the beginning of the Macro file to be debugged I add

Code: Select all

DEBUG = True

if DEBUG:
    import sys
    freecadpython = sys.executable.replace('freecad', 'python')
    App.Console.PrintMessage('Using ' + freecadpython +'\n')
    try:
        import debugpy
    except ModuleNotFoundError:
        App.Console.PrintMessage('Install debugpy by: ' + freecadpython + ' -m pip install --upgrade debugpy\n')
    #see https://github.com/microsoft/debugpy/issues/262
       
    debugpy.configure(python=freecadpython)
    if not debugpy.is_client_connected():
        debugpy.listen(5678)

    App.Console.PrintMessage("Waiting for debugger attach\n")
    debugpy.wait_for_client()
    debugpy.breakpoint()
Don't use disconnect in the debugger It seems to leave the server running in FreeCAD and it then complains the port is in use when you try to reconnect. Just let the macro terminate, edit it as required and rerun it. The debug session will restart without a wait for attach.
The script reminds me how to install debugpy in freecad's namespace if I haven't yet done it.
I didn't need any .env file. The debugger could find all FreeCAD's files and code completion works in the debug console.
I don't have a Workbench to debug, so this is a bit off-topic.
Post Reply