Debugging in FreeCAD
How to setup a system to debug FreeCAD python scripts.
Check system stability
In some Linux systems the debugger will crash. The problem is caused by in expat library. This is happens for example on Ubuntu 18.04. To find out, if our system has this problem, run two python lines below. If Freecad crashes, we need to fix our system first.
- Launch FreeCAD
- Normally, the python console is hidden. To show the console, go to Menu View->Panels->Python console.
- Now ran these lines below
from xml.dom import minidom minidom.parseString("<test></test>")
If FreeCAD crashes you need to fix it. We follow the instructions on . They suggest to downgrade the libexpat file:
For Ubuntu download Ubuntu 16.04 versions of libexpat1_2.1.0 from . These is usually libexpat1_2.1.0-7ubuntu0.16.04.3_amd64.deb (or libexpat1_2.1.0-7ubuntu0.16.04.3_i386.deb for 32 bit system)
Than go to your Download directory and install it with
sudo dpkg -i libexpat1_2.1.0-7ubuntu0.16.04.3_amd64.deb
Try again. If freecad does not crash, the debugger will probably work.
Install and the debugger
We need a python debugger Winpdb. If you did not installed before, install it with:
sudo apt-get install winpdb
Now lets setup the debugger.
- Start Winpdb.
- Set the debugger password to "test": Go to menu File->Password" and set the password.
Now we will run a test python script in FreeCAD step by step.
- Create a test script like from :
import rpdb2 rpdb2.start_embedded_debugger("test") import FreeCAD import Part import Draft print "hello" print "hello" import Draft points=[FreeCAD.Vector(-3.0,-1.0,0.0),FreeCAD.Vector(-2.0,0.0,0.0)] Draft.makeWire(points,closed=False,face=False,support=None)
- Open this script in FreeCAD and run it. FreeCAD will freeze, because the script line
waits until you attach the debugger.
- Attach the debugger. In Winpdb go to menu File->Attach. Winpdb will show you a window with your script or multiple script. Select your script and click OK.
Now you can execute the script line by line by clicking the "next"-Button from the Winpdb-Toolbar. Run all the lines. In the tab "Locals" you can see the values of the local variables.
- When you finish debugging, detach the debugger: Go to menu File->Detach.
Thank you to user Kunda1 from FreeCAD forum for the instructions how to fix debugger.