I'm tired of inserting
import pdb; pdb.set_trace()
lines into my Python programs and debugging through the console. How do I connect a remote debugger and insert breakpoints from a civilized user interface?
On the remote computer, find and start the Remote Debugger from the Start menu. If you don't have administrative permissions on the remote computer, right-click the Remote Debugger app and select Run as administrator. Otherwise, just start it normally.
Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
Just use python -m pdb <your_script>. py then b <line_number> to set the breakpoint at chosen line number (no function parentheses). Hit c to continue to your breakpoint. You can see all your breakpoints using b command by itself.
use Winpdb. It is a platform independent graphical GPL Python debugger with support for remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.
Features:
(source: winpdb.org)
Well, you can get something quite similar to that using a twisted manhole, which works like this:
from twisted.internet import reactor
from twisted.cred import portal, checkers
from twisted.conch import manhole, manhole_ssh
def getManholeFactory(namespace):
realm = manhole_ssh.TerminalRealm()
def getManhole(_):
return manhole.Manhole(namespace)
realm.chainedProtocolFactory.protocolFactory = getManhole
p = portal.Portal(realm)
p.registerChecker(
checkers.InMemoryUsernamePassword DatabaseDontUse(admin='foobar'))
f = manhole_ssh.ConchFactory(p)
return f
reactor.listenTCP(2222, getManholeFactory(globals()))
reactor.run()
Then you just login to the program over ssh;
$ ssh admin@localhost -p 2222
admin@localhost's password:
Using foobar as the password.
When you login you'll get a normal python prompt where you can just poke at the data. It's not quite the same as getting a traceback sent over to a host.
Now, this might be tricky to integrate to a GUI program, in that case you might need to choose another reactor, for instance for gtk based programs used the gtk2reactor etc.
If you want the actual traceback sent over you need to create a socket channel for both stderr, stdin and stdout which goes over the network instead of printing to your local host. Shouldn't be too hard to accomplish by using twisted.
A little bit late, but here is a very lightweight remote debugging solution courtesy of http://michaeldehaan.net/post/35403909347/tips-on-using-debuggers-with-ansible:
pip install epdb
on the remote host.epdb
defaults to listening on any address (INADDR_ANY
), not 127.0.0.1.import pdb; pdb.set_trace()
in your program, use import epdb; epdb.serve()
.epdb.connect()
uses telnet.python -c 'import epdb; epdb.connect()'
.Adjust the security bits to suit your local network setup and security stance, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With