[Solved] Setting up vscode to debug python scripts

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

[Solved] Setting up vscode to debug python scripts

Post by drmacro »

I have in the past setup and been successful at debugging FreeCAD python scripts with vscode.

Haven't done it in a while and need to re-do after vscode update.

I've got the json debug config setup and can get FC to connect with vscode.

But, vscode can't find FC code. I think this is because with no other configuration it is looking in the directory where the .py file is rather than the freecad directory.

So, I think I need to know how to tell vscode additional search path. But how?

Edit: figured it out.I have in the past setup and been successful at debugging FreeCAD python scripts with vscode.

Haven't done it in a while and need to re-do after vscode update.

I've got the json debug config setup and can get FC to connect with vscode.

But, vscode can't find FC code. I think this is because with no other configuration it is looking in the directory where the .py file is rather than the freecad directory.

So, I think I need to know how to tell vscode additional search path. But how?

Edit: figured it out. vscode is really complaining that it can't find the file being debugged with error: "Breakpoint in non-existant file". Even though the file is open in vscode. But the debug session is actually connecting to FC so the link doesn't find the file being executed.

The launch.json file has to reflect where the file being debugged exists.
Example: File being debugged is /home/mac/SharedData/FC_common/ListSelection-3.py

launch.json needs to specify remoteRoot as follows:

Code: Select all

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Attacher",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/home/mac/SharedData/FC_common"
                }
            ]
        }
    ]
}
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Setting up vscode to debug python scripts

Post by edwilliams16 »

Being a noob at python, FreeCAD, VSCode, json, git and open-source development in general, take my advice for what it is worth. In the last couple of days I've random-walked towards a working configuration for debugging Macros and Workbenches.
I'm using the Mac "app", so the directories in question are reachable, but "hidden" in various ways" (shift-command-G in the file selection box allows you to get into hidden directory paths)

I've described some of that in an earlier thread.
One recent change I made was to point "remoteRoot" in .vscode/launch.json at the directory in which the python files I'm debugging live. This fixed some "unknown file" errors.

Code: Select all

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: FreeCAD Attacher",
            "type": "python",
            "request": "attach",
            "redirectOutput": true,
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/Users/ed/Library/Preferences/FreeCAD/Mod/OpenSCAD_Alt_Import"
                }
            ]
        }
    ]
}
Perhaps also important is to point the python in .vscode/settings.json at FreeCAD's embedded python

Code: Select all

{
    "python.pythonPath": "/Applications/FreeCAD.app/Contents/Resources/bin/python"
}
I found the video https://www.youtube.com/watch?v=06I63_p-2A4 illuminating - though sometimes overly concerned with cosmetics. I need to go through it a second time and decide which ideas to adopt.

My output redirect from FreeCAD to the debug console is now working.

I was initially reluctant to make my development directory FreeCAD's - but many Mod and Macro subdirectories are already git repositories, or can be made so. So I no longer soft-link FreeCAD into my development directory, I work in situ on a new git brach that I can safely revert to working code.
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Solved] Setting up vscode to debug python scripts

Post by edwilliams16 »

Looks like you found the solution I found.
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Solved] Setting up vscode to debug python scripts

Post by edwilliams16 »

drmacro wrote: Sat Apr 17, 2021 1:00 pm I have in the past setup and been successful at debugging FreeCAD python scripts with vscode.
Did you find the right way/sequence to reload a macro or workbench while debugging?

Code: Select all

from importlib import reload
reload(MACRO)
in the FreeCAD python console works for me when I'm not using the debugger, but I get crashes/not founds in the ways I've tried while debugging. It's a pain to have restart FreeCAD every time I fix a typo in the source I'm working on.
drmacro
Veteran
Posts: 8870
Joined: Sun Mar 02, 2014 4:35 pm

Re: [Solved] Setting up vscode to debug python scripts

Post by drmacro »

I modify the code in vscode, save and just run it again.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3112
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Solved] Setting up vscode to debug python scripts

Post by edwilliams16 »

drmacro wrote: Sun Apr 18, 2021 10:45 pm I modify the code in vscode, save and just run it again.
I think that is good for Macros, but not workbenches. I believe Python keeps track of what has been imported already, so importing a file that has been changed does nothing. At least, that's how it has been working for me.
Post Reply