Python workbenches debugging

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Zoltan
Posts: 62
Joined: Wed Jul 07, 2021 10:08 am

Re: Python workbenches debugging

Post by Zoltan »

hlg wrote: Fri Jul 12, 2019 10:30 am However you don't need remote debugging as long as PyCharm and the FreeCAD application are running on the same machine. You can just attach the remote debugger to a running FreeCAD instance and off you go. In PyCharm this can be found under Run / Attach To Process or with Ctrl-Alt-F5. You are presented with a list of processes to select from. You can configure the processes to appear in this list under File / Settings / Build, Execution, Deployment / Python Debugger. There you can specify a string. All the processes containing this string in their name will appear in the process list to select from later on. The initial search string is "python" such that FreeCAD will not appear in the list. Changing it to "FreeCAD" should work. When in doubt, inspect the list of running processes with the tools of your OS and also note the process ID.
Hi @hlg,

I followed your instructions with the community edition of PyCharm but I couldn't attach the debugger. First, I found the process ID of the running FreeCAD instance, and then tried to connect. The error I received is below. I think the problem is that FreeCAD does not recognize the `--port` flag. How did you solve it? Another question: when you change the values of variables or create new objects in debug mode, are the changes reflected in the running FreeCAD instance the debugger is attached to? Thank you in advance.
Attachments
PyCharm.png
PyCharm.png (93.16 KiB) Viewed 3103 times
User avatar
jnxd
Posts: 951
Joined: Mon Mar 30, 2015 2:30 pm
Contact:

Re: Python workbenches debugging

Post by jnxd »

steelman wrote: Tue Apr 20, 2021 11:04 am I open FreeCAD. I enter the following into the console to load my version of Arch workbench (especially ArchWindow module)

Code: Select all

import importlib # to use reload() later 
sys.path.insert(0, "/home/steelman/src/FreeCAD/src/Mod/Arch")
for m in [k for k in sys.modules.keys() if 'Arch' in k]: sys.modules.pop(m, None)
import Arch
When I try to use pdb it either does not set a breakpoint

Code: Select all

>>> import pdb
>>> pdb.set_trace()
--Return--
> <input>(1)<module>()->None
(Pdb) b ArchWindow:683
(Pdb) c
>>> 
or it stops accepting any input except Enter key (which sets consequent copies of the breakpoint )

Code: Select all

 >>> import ArchWindow
>>> pdb.set_trace()
--Return--
> <input>(1)<module>()->None
(Pdb) b ArchWindow:683
Breakpoint 1 at /home/steelman/src/FreeCAD/src/Mod/Arch/ArchWindow.py:683
(Pdb) c
Breakpoint 2 at /home/steelman/src/FreeCAD/src/Mod/Arch/ArchWindow.py:683
(Pdb) 
PS. To reload ArchWindow I am working on, I use the following commands:

Code: Select all

sys.modules.pop('ArchWindow') # to unload, otherwise reloading Arch is no-op
importlib.reload(Arch)
EDIT:

Putting import pdb; pdb.set_trace() in the code works only slightly better. Although I can interact with pdb, it's really awkward because I write my commands in the console and get results in the report view. To make things worse pressing Enter sends a command to pdb but does not remove it from the command line.
Were you able to resolve this issue, steelman? I'm having the same issue on a debug build of freecad.
My latest (or last) project: B-spline Construction Project.
steelman
Posts: 48
Joined: Wed Jul 05, 2017 9:25 am

Re: Python workbenches debugging

Post by steelman »

jnxd wrote: Fri Jul 09, 2021 12:57 am Were you able to resolve this issue, steelman? I'm having the same issue on a debug build of freecad.
Nope.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Python workbenches debugging

Post by Kunda1 »

bump
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
User avatar
hlg
Posts: 39
Joined: Fri Jul 12, 2019 10:11 am

Re: Python workbenches debugging

Post by hlg »

Zoltan wrote: Wed Jul 07, 2021 10:22 am I think the problem is that FreeCAD does not recognize the `--port` flag. How did you solve it?
I have not seen this issue, but I have only used the PyCharm debugging on Windows so far and also not tried with the latest FreeCAD version. I will try and see whether I can reproduce this behavior on Ubuntu.

Zoltan wrote: Wed Jul 07, 2021 10:22 am Another question: when you change the values of variables or create new objects in debug mode, are the changes reflected in the running FreeCAD instance the debugger is attached to? Thank you in advance.
When you edit the code of a workbench loaded in FreeCAD, the changes will not be reflected unless you reload the workbench. See the initial post of this thread for further details on reloading a workbench.
keithsloan52
Veteran
Posts: 2756
Joined: Mon Feb 27, 2012 5:31 pm

Re: Python workbenches debugging

Post by keithsloan52 »

Any debuggers for MacOS?
User avatar
hlg
Posts: 39
Joined: Fri Jul 12, 2019 10:11 am

Re: Python workbenches debugging

Post by hlg »

I think I figured out what is happening with PyCharm on Ubuntu. Usually, it should be possible to attach to processes with embedded Python, not just Python interpreters (PyCharm issue 14181). But it seems this is broken, since it currently tries to use the executable (e.g. FreeCAD) as Python interpreter to run the script that attaches to the process ("attach_pydevd.py"). The arguments (--port and --pid) would normally be handed to the script by the Python interpreter, but FreeCAD does not act like a Python interpreter and cannot digest this. There seems to be a similar issue with attaching to Maya on MacOS (PyCharm issue 49110). Maybe an older PyCharm version does not have this issue or we need to wait for Jetbrains to fix this.
Zoltan
Posts: 62
Joined: Wed Jul 07, 2021 10:08 am

Re: Python workbenches debugging

Post by Zoltan »

Thank you @hlg for the explanation and that you communicated this issue towards JetBrains. A real solution would be a bug-fix, as I don't want to use an ancient version of PyCharm due to the useful new features and my plugins.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Python workbenches debugging

Post by heda »

Post Reply