Debug Python

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!
jairds
Posts: 33
Joined: Mon Mar 18, 2013 7:20 pm

Debug Python

Post by jairds »

How to debug Python in VS2008?

I found python tools for visual studio - http://pytools.codeplex.com/wikipage?ti ... Title=Home but they are available only for VS2010/2012. I am building Freecad with VS2008. What's the best way to step through Python code inside it (if possible).

Thank you.
Jair Santos
crashfridh
Posts: 143
Joined: Thu Jan 03, 2013 1:42 pm

Re: Debug Python

Post by crashfridh »

Python has its own debugger in the module pdb.
To graphically debug one can use ddd. I haven't used this combination myself though, since Python shows excellent traces to work with.
Frederik
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Debug Python

Post by wmayer »

jairds
Posts: 33
Joined: Mon Mar 18, 2013 7:20 pm

Re: Debug Python

Post by jairds »

Thank you guys.

I've been trying to use winpdb without success and would like to ask for some help.

When I run FC I notice in the python console. Python 2.6.2 (r262:71600, Jun 22 2009, 21:41:42) [MSC v.1500 32 bit (Intel)] on win32

The winpdb readme.txt says " Winpdb is compatible with CPython 2.3 or later. Winpdb is NOT
compatible with Jython or IronPython."

I assume that CPython = Python, is that right?

Then I installed winpdb and got:

C:\Python27\winpdb-1.4.8>python setup.py install -f
running install
running build
running build_py
creating build
creating build\lib
copying winpdb.py -> build\lib
copying rpdb2.py -> build\lib
running build_scripts
creating build\scripts-2.6
copying winpdb_inst.py -> build\scripts-2.6
copying and adjusting winpdb_.pyw -> build\scripts-2.6
copying winpdb.bat -> build\scripts-2.6
copying rpdb2.bat -> build\scripts-2.6
running install_lib
creating Lib
creating Lib\site-packages
copying build\lib\rpdb2.py -> Lib\site-packages
copying build\lib\winpdb.py -> Lib\site-packages
byte-compiling Lib\site-packages\rpdb2.py to rpdb2.pyc
byte-compiling Lib\site-packages\winpdb.py to winpdb.p
running install_scripts
creating Scripts
copying build\scripts-2.6\rpdb2.bat -> Scripts
copying build\scripts-2.6\winpdb.bat -> Scripts
copying build\scripts-2.6\winpdb_.pyw -> Scripts
copying build\scripts-2.6\winpdb_inst.py -> Scripts
running install_egg_info
Writing Lib\site-packages\winpdb-1.4.8-py2.6.egg-info

It seems that it is OK.

To test I tried to debug the FC script importDXF.py, so I copied the line "import rpdb2; rpdb2.start_embedded_debugger(password) " to it. The instructions from http://winpdb.org/docs/embedded-debugging/ says that the script will freeze for a default period of 5 minutes, waiting for a debugger to attach.

When I open a DXF file with FC it works, meaning that it opened the file but winpdb does not froze the script as it should.

What am I missing?

Regards.
Jair Santos
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Debug Python

Post by wmayer »

I assume that CPython = Python, is that right?
Yep.
What am I missing?
And the script you want to debug did you set some breakpoints there?
jairds
Posts: 33
Joined: Mon Mar 18, 2013 7:20 pm

Re: Debug Python

Post by jairds »

Thanks.

I set breakpoints but when I run FC they turn to disabled, and a message that there is no symbols have been loaded for this document show up. Needless to say that I built the solution in debug mode.

Regards.

Jair
Jair Santos
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Debug Python

Post by wmayer »

I set breakpoints but when I run FC they turn to disabled, and a message that there is no symbols have been loaded for this document show up. Needless to say that I built the solution in debug mode.
Ahem, ahem, ahem, I guess you mix something up. "Symbols loaded" is totally irrelevant for Python because with the .py file you already have the whole source code. "Symbols loaded" is relevant for C++.

The steps to debug a Python script are as follows:
1. Open a terminal window and change to the directory of your Winpdb installation. Start it with:

Code: Select all

python winpdb.py
2. In Winpdb go to File>Attach and set a password. e.g. "test"

3. Create a test script with these lines:

Code: Select all

import rpdb2
rpdb2.start_embedded_debugger("test")

import FreeCAD

FreeCAD.Console.PrintMessage("set a breakpoint here!")
FreeCAD.Console.PrintMessage("Hello, ")
FreeCAD.Console.PrintMessage("World!")
4. Start FreeCAD and run the above script.

5. Go back to Winpdb and press the Refresh button on the Attach panel. You'll see the process id of the application that tries to connect to Winpdb and the file that it executes. Select it and press OK.

6. The executed Python file should show up in Winpdb on the right side. Now you can set breakpoints and start the debugging with F5. With F6 you can do it step-by-step.

Note: As long as FreeCAD is connected to Winpdb it is inaccessible. You have to detach it from Winpdb to make it responsive again.
jairds
Posts: 33
Joined: Mon Mar 18, 2013 7:20 pm

Re: Debug Python

Post by jairds »

Thanks. I tried it but I got


C:\Winpdbsource\winpdb-1.4.8>python winpdb
Winpdb is being run with the wrong version of Python.
Winpdb path: C:\Winpdbsource\winpdb-1.4.8\winpdb
Python path: C:\FreeCADLibs_8.0_x86_VC9\bin\python.exeC:\Winpdbsource\winpdb-1.
.8\rpdb2.py:308: DeprecationWarning: the sets module is deprecated
import sets
C:\Winpdbsource\winpdb-1.4.8\rpdb2.py:313: DeprecationWarning: The popen2 modul
is deprecated. Use the subprocess module.
import popen2
wxPython was not found.
wxPython 2.6 or higher is required to run the winpdb GUI.
wxPython is the graphical user interface toolkit used by Winpdb.
You can find more information on wxPython at http://www.wxpython.org/
The Unicode version of wxPython is recommended for Winpdb.
To use the debugger without a GUI, run rpdb2.

I have


C:\FreeCADLibs_8.0_x86_VC9\bin>python
Python 2.6.2 (r262:71600, Jun 22 2009, 21:44:05) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Jair Santos
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Debug Python

Post by wmayer »

Do you have wxPython installed? From http://www.wxpython.org/download.php you can get a version which is compiled against Python 2.6.
jairds
Posts: 33
Joined: Mon Mar 18, 2013 7:20 pm

Re: Debug Python

Post by jairds »

Thanks.

I didn't have Winpdb installed so I installed it. Then I opened the console and I got:

C:\Winpdbsource\winpdb-1.4.8>python winpdb.py
C:\Winpdbsource\winpdb-1.4.8\rpdb2.py:308: DeprecationWarning: the sets module i
s deprecated
import sets
C:\Winpdbsource\winpdb-1.4.8\rpdb2.py:313: DeprecationWarning: The popen2 module
is deprecated. Use the subprocess module.
import popen2

Right after, the gui opened and I set the password.

When I tried to run the script on Freecad I got some errors :

>>> import rpdb2
Traceback (most recent call last):
File "<input>", line 1, in <module>
ImportError: No module named rpdb2
>>> rpdb2.start_embedded_debugger("test")
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'rpdb2' is not defined
>>>
>>> import FreeCAD
>>>
>>> FreeCAD.Console.PrintMessage("set a breakpoint here!")
>>> FreeCAD.Console.PrintMessage("Hello, ")
>>> FreeCAD.Console.PrintMessage("World!")

I thought it was missing the Winpdb PATH in the environments variables so I set it all over the place (including in PYTHONPATH). I tried to pass the full path of rpdb2 but it is still not finding the file.

Regards
Jair Santos
Post Reply