Debugging python code.

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Debugging python code.

Postby keithsloan52 » Fri Apr 20, 2018 8:08 pm

Up until now, I have debugged python code by adding print statements to work out what is going on.

Is there something like a symbolic debugger that can be used with FreeCAD Python code?
ezzieyguywuf
Posts: 542
Joined: Tue May 19, 2015 1:11 am

Re: Debugging python code.

Postby ezzieyguywuf » Fri Apr 20, 2018 8:25 pm

pdb is helpful for this. Instead of using print statements, use import pdb; pdb.set_trace(). Now, the code will stop there and drop you into a prompt, where you can inspect variables and step through the code.
User avatar
Kunda1
Posts: 5509
Joined: Thu Jan 05, 2017 9:03 pm

Re: Debugging python code.

Postby Kunda1 » Fri Apr 20, 2018 9:26 pm

Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Re: Debugging python code.

Postby keithsloan52 » Sat Apr 21, 2018 7:11 pm

ezzieyguywuf wrote:
Fri Apr 20, 2018 8:25 pm
pdb is helpful for this. Instead of using print statements, use import pdb; pdb.set_trace(). Now, the code will stop there and drop you into a prompt, where you can inspect variables and step through the code.
Tried this but it does not appear to invoke the debugger when it hits the pdb.set_trace(). Least does not open a debugging window as far as I can tell

OS: Mac OS X
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13509 (Git)
Build type: Release
Branch: tag: 0.17
Hash: 0258808ccb6ba3bd5ea9312f79cd023f1a8671b7
Python version: 2.7.14
Qt version: 5.10.1
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: English/UnitedKingdom (en_GB)

Following @Kunda1 post the debugger needed changes to FreeCAD and I am wondering if these ever made it to FreeCAD 0.17
@mumme
wmayer
Site Admin
Posts: 14782
Joined: Thu Feb 19, 2009 10:32 am

Re: Debugging python code.

Postby wmayer » Sat Apr 21, 2018 8:19 pm

Tried this but it does not appear to invoke the debugger when it hits the pdb.set_trace(). Least does not open a debugging window as far as I can tell
pdb is very low-level debugger and doesn't offer any GUI. You more or less can compare it gdb to debug C/C++ code of gcc where you do everything from the command line.
If you want something high-level then using Winpdb might be an option. Just search for it in this forum.
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Re: Debugging python code.

Postby keithsloan52 » Sat Apr 21, 2018 8:45 pm

Quite happy with the equivalent of a command line debugger like gdb but when I add

Code: Select all

import pdb; pdb.set_trace()
Nothing seems to happen when it reaches the pdb.set_trace.

I was expecting a window with a command line interface from what I read. Checked python console & report view but nothing.

winpdb does not seem to be an option. I don't have windows and my Linux machine died with a power supply failure.
So need something to work on the Mac
wmayer
Site Admin
Posts: 14782
Joined: Thu Feb 19, 2009 10:32 am

Re: Debugging python code.

Postby wmayer » Sat Apr 21, 2018 8:52 pm

winpdb does not seem to be an option. I don't have windows and my Linux machine died with a power supply failure.
So need something to work on the Mac
Winpdb itself is written in Python and all what it needs is wxWidgets and its Python binding. This should also be available on macOS.
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Re: Debugging python code.

Postby keithsloan52 » Sat Apr 21, 2018 9:23 pm

wmayer wrote:
Sat Apr 21, 2018 8:52 pm
Winpdb itself is written in Python and all what it needs is wxWidgets and its Python binding. This should also be available on macOS.
Okay as per http://macappstore.org/wxmac/
I ran

Code: Select all

brew install wxmac
and was told wxmac 3.0.4 is already installed and up-to-date
To reinstall 3.0.4, run `brew reinstall wxmac`

I assume I have to download a source version of winpdb and somehow link it into python.
Not sure how to do the later.
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Re: Debugging python code.

Postby keithsloan52 » Sat Apr 21, 2018 9:29 pm

Okay I guess I just download the source and run the install script as per Linux.

I will give it a try.
keithsloan52
Posts: 1009
Joined: Mon Feb 27, 2012 5:31 pm

Re: Debugging python code.

Postby keithsloan52 » Sun Apr 22, 2018 5:21 pm

Okay I have winpdb installed.

I tried to follow https://www.freecadweb.org/wiki/Debuggi ... _Debugging
and created a test.py file as

Code: Select all

import rpdb2
rpdb2.start_embedded_debugger("test")
import FreeCAD
open("/Users/keith/Desktop/Problem/test2.csg")
After I load it into FreeCAD and press f6
I get an error

Code: Select all

import rpdb2
ImportError: No module named rpdb2
I assume that I have to copy something to do with rpdb2 to somewhere in FreeCAD