[Mostly Solved] Debug with MacOS VScode

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

[Mostly Solved] Debug with MacOS VScode

Post by edwilliams16 »

Code: Select all

13:00:25  Could not connect to 127.0.0.1: 51390
13:00:25  Traceback (most recent call last):
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 464, in start_client
    s.connect((host, port))
13:00:25  ConnectionRefusedError: [Errno 61] Connection refused
13:00:25  Traceback (most recent call last):
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/../debugpy/server/attach_pid_injected.py", line 79, in attach
    debugpy.connect(
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/common/compat.py", line 210, in kwonly_f
    return f(*args, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/__init__.py", line 135, in connect
    return api.connect(address, access_token=access_token)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 143, in debug
    log.reraise_exception("{0}() failed:", func.__name__, level="info")
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 141, in debug
    return func(address, settrace_kwargs, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 276, in connect
    _settrace(host=host, port=port, client_access_token=access_token, **settrace_kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 47, in _settrace
    return pydevd.settrace(*args, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2607, in settrace
    _locked_settrace(
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2688, in _locked_settrace
    py_db.connect(host, port)  # Note: connect can raise error.
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 1262, in connect
    s = start_client(host, port)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 464, in start_client
    s.connect((host, port))
13:00:25  ConnectionRefusedError: [Errno 61] Connection refused
13:00:25  E+03560.975: Traceback (most recent call last):
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/../debugpy/server/attach_pid_injected.py", line 79, in attach
                 debugpy.connect(
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/common/compat.py", line 210, in kwonly_f
                 return f(*args, **kwargs)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/__init__.py", line 135, in connect
                 return api.connect(address, access_token=access_token)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 143, in debug
                 log.reraise_exception("{0}() failed:", func.__name__, level="info")
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 141, in debug
                 return func(address, settrace_kwargs, **kwargs)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 276, in connect
                 _settrace(host=host, port=port, client_access_token=access_token, **settrace_kwargs)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 47, in _settrace
                 return pydevd.settrace(*args, **kwargs)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2607, in settrace
                 _locked_settrace(
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2688, in _locked_settrace
                 py_db.connect(host, port)  # Note: connect can raise error.
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 1262, in connect
                 s = start_client(host, port)
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 464, in start_client
                 s.connect((host, port))
             ConnectionRefusedError: [Errno 61] Connection refused
             
             Stack where logged:
               File "<string>", line 1, in <module>
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/../debugpy/server/attach_pid_injected.py", line 92, in attach
                 log.reraise_exception()
               File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/common/log.py", line 226, in reraise_exception
                 _exception(format_string, *args, **kwargs)
             

13:00:25  Traceback (most recent call last):
13:00:25    File "<string>", line 1, in <module>
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/../debugpy/server/attach_pid_injected.py", line 92, in attach
13:00:25      log.reraise_exception()
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/../debugpy/server/attach_pid_injected.py", line 79, in attach
13:00:25      debugpy.connect(
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/common/compat.py", line 210, in kwonly_f
13:00:25      return f(*args, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/__init__.py", line 135, in connect
13:00:25      return api.connect(address, access_token=access_token)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 143, in debug
13:00:25      log.reraise_exception("{0}() failed:", func.__name__, level="info")
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 141, in debug
13:00:25      return func(address, settrace_kwargs, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 276, in connect
13:00:25      _settrace(host=host, port=port, client_access_token=access_token, **settrace_kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/server/api.py", line 47, in _settrace
13:00:25      return pydevd.settrace(*args, **kwargs)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2607, in settrace
13:00:25      _locked_settrace(
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 2688, in _locked_settrace
13:00:25      py_db.connect(host, port)  # Note: connect can raise error.
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/pydevd.py", line 1262, in connect
13:00:25      s = start_client(host, port)
13:00:25    File "/Users/ed/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 464, in start_client
13:00:25      s.connect((host, port))
13:00:25  ConnectionRefusedError: [Errno 61] Connection refused
OS: macOS High Sierra (10.13)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.24276 (Git)
Build type: Release
Branch: (HEAD detached at 0.19.1)
Hash: a88db11e0a908f6e38f92bfc5187b13ebe470438
Python version: 3.8.8
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.4.0
Locale: C/Default (C)

VSCode
Version: 1.55.1 (Universal)
Commit: 08a217c4d27a02a5bcde898fd7981bda5b49391b
Date: 2021-04-07T18:22:52.186Z
Electron: 11.3.0
Chrome: 87.0.4280.141
Node.js: 12.18.3
V8: 8.7.220.31-electron.0
OS: Darwin x64 17.7.0

I'm following https://forum.freecadweb.org/viewtopic.php?f=22&t=28901 and optimistically hoping that by 2021 I can debug Macros on a Mac with Visual Studio.
When I try to attach the debugger to the FreeCAD process, I get the above errors in the report console, which appear to say that the connection was refused. Is there a way around this? Give permissions, somewhere?

I think I got the right Python executable in VSCODE: /Applications/FreeCAD.app/Contents/Resources/bin/python
It has a path

Code: Select all

>>> sys.path
['', '/Applications/FreeCAD.app/Contents/Resources/lib/python38.zip', '/Applications/FreeCAD.app/Contents/Resources/lib/python3.8', '/Applications/FreeCAD.app/Contents/Resources/lib/python3.8/lib-dynload', '/Applications/FreeCAD.app/Contents/Resources/lib/python3.8/site-packages']
>>> 
which has package stuff I assume, but maybe I have to add Macro folders at some point which are under ~/Library/Preferences/FreeCAD/ or does FreeCAD set its own python path somewhere?

I'm lost here...
Last edited by edwilliams16 on Tue Apr 13, 2021 11:07 pm, edited 1 time in total.
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Debug with MacOS VScode

Post by edwilliams16 »

The wiki has some instructions using the now-deprecated ptvsd https://wiki.freecadweb.org/Debugging

With some minor modifications, I got it working. First problem was even using FreeCAD"s own packaged python pip install ptvsd installed off FreeCAD's python path. I fixed this by appending to the path at the beginning of the macro. Output to the debug console gave deprecation warnings, which I got rid of by moving the instruction to the launch.json file.

So here's a working configuration:

Test macro file with necessary preamble

Code: Select all

from sys import path
sys.path.append('/Users/ed/opt/anaconda3/lib/python3.7/site-packages')
import ptvsd
print("Waiting for debugger attach")
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.wait_for_attach()
#end preamble
import FreeCAD
import Part
x,y,z = 3,5,7
print('x=' + str(x)) # to debug console
box = Part.makeBox(x,y,z)
Part.show(box)
And a launch.json (Couldn't find an Add Configuration, so changed an existing one. Changing the "name" attribute seems to work to make a new configuration, not clobbering the old one.)

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",
            "port": 5678,
            "host": "localhost",
            "redirectOutput": true,
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
    ]
}
The re-direction only seems to work the first time you run the macro - haven't figured that out. It would also seem desirable to switch to ptvsd's successor debugpy https://pypi.org/project/debugpy/ - but that's for another day.

Finding where your system python installed ptvsd:

Code: Select all

>>> import ptvsd
>>> print(ptvsd.__file__)
/Users/ed/opt/anaconda3/lib/python3.7/site-packages/ptvsd/__init__.py
>>> 
So it lives in /Users/ed/opt/anaconda3/lib/python3.7/site-packages/
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Mostly Solved] Debug with MacOS VScode

Post by edwilliams16 »

I found a way to use Macro|Attach to Remote Debugger without having to modify the Macro you are debugging or typing into FreeCAD's python console . This gets around the bug that FreeCAD can't find ptvsd the debugger module.

Load the Macro to be debugged into VSCode's editor.
Edit a launch.json to read

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",
            "port": 5678,
            "host": "localhost",
            "redirectOutput": true,
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        },
    ]
}

Code: Select all

pip install ptvsd
If you are lucky, typing

Code: Select all

import ptvsd
into FreeCAD's python console meets with success. If so skip the next step.

Create a new Macro import_ptvsd.FCMacro

Code: Select all

from sys import path
path.append('/path/to/where/ptvsd/was installed')
import ptvsd
then run the Macro.
  • Go to Macro|Attach to Remote Debugger choose VSCode and the default arguments. Clicking OK will freeze FreeCAD.
  • Go to VSCode and press F5 (Run). FreeCAD will unfreeze.
  • Set breakpoints, whatever in your Macro in VSCode.
  • Run the Macro in FreeCAD.
You can now step through, watch your variables etc.

The output is NOT as yet redirected from the Report View to the debug console.
Post Reply