Debugging a python script

296 views
Skip to first unread message

Mike

unread,
Mar 27, 2017, 12:16:38 PM3/27/17
to Jep Project
Hi,

If I have an external python script that I am using Jep to call through to (using getValue) in my java process, is there a way to step through the python script line by line as it is being interpreted in java (attaching a python IDE and setting break points etc.)?

Regards,
Mike

Nathan Jensen

unread,
Mar 28, 2017, 3:50:40 PM3/28/17
to Jep Project
This has been done before but this solution is not the cleanest thing ever.  I'm not endorsing this solution, if it doesn't work, I don't know why, but people have gotten it to work before.  If anyone can come up with a solid solution then I'll put that on the wiki.  Please let us know if you get this to work or come up with an alternative.

PyDev is a Python IDE that is a set of Eclipse plugins.  It has a debug capability similar to Eclipse's attach to a remote JVM where it can attach to a process.  Try the following steps:

1. Setup Python Interpreter in Eclipse if not done yet.

    Window->Preferences->PyDev->Interpreters

2. Determine your PYDEVD_PATH

    e.g. ~/eclipse/plugins/org.python.pydev_5.3.1.201610311318/pysrc

3. Start the PyDev Debug server in eclipse.

    Window->Customize Perspective->Tool Bar Visibility->PyDev Debug
    Window->Customize Perspective->Menu Visibility->Pydev
    Window->Customize Perspective->Command Groups Availability->PyDev Debug

4. Add the following method to the python module you wish to debug either class level or module level.

def pydevDebug(self):
    import sys
    PYDEVD_PATH='the PYDEVD_PATH determined earlier'
    if sys.path.count(PYDEVD_PATH) < 1:
        sys.path.append(PYDEVD_PATH)
    import pydevd
    pydevd.settrace()

5. Add a call(s) to above method where you want to break in the code.
6. Redeploy the updated python module.
7. In eclipse, start the PyDev Debug Server
8. Trigger the stack trace
9. Step into pydevd.settrace()



--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/4db6581f-b433-4bec-b947-12a8bf1d07af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike

unread,
Mar 29, 2017, 8:14:33 AM3/29/17
to Jep Project
Thanks Nathan,

I have PyCharm which also supports this remote debugging method. I have just tried this and it worked.
My steps were slightly different so I'll post them here for others to use:
  • Download the relevant pydevd library from pypi (can also be obtained from PyDev or PyCharm installation locations but I wanted to see if this could also be deployed separately)
  • In PyCharm create a run configuration for the Python Remote Debug (probably a similar item in PyDev)
  • Start the PyCharm debug server. This will give you output similar to the following:
Starting debug server at port 52190
Waiting for process connection...
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('localhost', port=52190, stdoutToServer=True, stderrToServer=True)
  •  In your python script add the pydevd import:
import pydevd
  • At the point you want to break in your script add the line the Python Remote Debug gave you above: 
... 
pydevd.settrace('localhost', port=52190, stdoutToServer=True, stderrToServer=True)
...
  • Run the script via Jep and the breakpoint will be hit.
Thanks for your help.
Mike 
 
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

Xavier Artusi

unread,
Apr 1, 2017, 2:35:13 AM4/1/17
to Jep Project
Thanks very much you spared me so much time.

The method works very well with eclipse.

I use a flag -DDEBUG_PYTHON when I start java application
And jep launchs import pydevd and pydevd.settrace()

Then break point set with eclipse in python scripts are triggered.

The only drawback is the need to start a python debug server
If not the application freezes until a debug server starts

Anyone know if it's possible to start a debug server from java, python or cmd?
(Without hitting the eclipse shortcut)

Greetings
Xavier.

Reply all
Reply to author
Forward
0 new messages