How do I attach a remote debugger to a Python process?
Asked Answered
Y

6

57

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?

Yulandayule answered 12/2, 2009 at 20:54 Comment(0)
M
55

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:

  • GPL license. Winpdb is Free Software.
  • Compatible with CPython 2.3 through 2.6 and Python 3000
  • Compatible with wxPython 2.6 through 2.8
  • Platform independent, and tested on Ubuntu Gutsy and Windows XP.
  • User Interfaces: rpdb2 is console based, while winpdb requires wxPython 2.6 or later.

Screenshot
(source: winpdb.org)

winpdb-reborn · PyPI

GitHub - bluebird75/winpdb: Fork of the official winpdb with improvements

Monachism answered 13/2, 2009 at 5:32 Comment(3)
sudo aptitude install winpdbDestruct
As of now, the original winpdb is not maintained and the website is dead. However, it has been forked: hereOphthalmic
unfortunately the forked winpdb doesn't work too well with python3Brownie
G
20

A little bit late, but here is a very lightweight remote debugging solution courtesy of
Michael DeHaan • Tips on Using Debuggers With Ansible:

  1. pip install epdb on the remote host.
  2. Make sure your firewalling setup is not allowing non-local connections to port 8080 on the remote host, since epdb defaults to listening on any address (INADDR_ANY), not 127.0.0.1.
  3. Instead of using import pdb; pdb.set_trace() in your program, use import epdb; epdb.serve().
  4. Securely log in to the remote host, since epdb.connect() uses telnet.
  5. Attach to the program using python -c 'import epdb; epdb.connect()'.

Adjust the security bits to suit your local network setup and security stance, of course.

Galyak answered 4/4, 2015 at 21:55 Comment(5)
This helped me to debug a python app that's running inside a Docker container. The setup was further complicated by the use of Docker Compose and nginx + uwsgi to run the python app so none of the other solutions worked for me. This one did.Prater
This is the slickest way to debug curses applications. Cheers!Translocation
Does this still work? I get module 'epdb' has no attribute 'serve'Aquiculture
@Aquiculture Any chance you're using github.com/native-human/epdb and not pypi.org/project/epdb ?Felting
Apologies for the only-very-slightly late reply, this still works for me nowadays. pip install epdb from a python 3.5.2 venv with pip 19.2.1 gets me epdb 0.15.1 from github.com/sassoftware/epdb, and the epdb.serve() and epdb.connect() methods still work as indicated in the answer above.Galyak
H
19

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.

Haberman answered 12/2, 2009 at 21:7 Comment(0)
J
3

Two solutions from modern IDEs:

  1. PTVS (Python tools for Visual Studio) cross-platform remote debugging

  2. PyCharm / PyDev remote debugging

Jacey answered 2/3, 2015 at 15:37 Comment(3)
Pycharm requires that it be Professional versionDysteleology
PTVS seems to have loads of issues, esp with containers. And CPython is not supported?Pinkeye
You also cannot attach to a remote process using PyCharm's remote debugger (see: youtrack.jetbrains.com/issue/PY-14702)Barrow
R
1

I find pudb useful at emergency

pip install pudb

Project Description https://pypi.org/project/pudb/

Tutorial: https://vimeo.com/5255125

Ribwort answered 6/8, 2020 at 12:9 Comment(0)
B
1

None of the answers here handle the case where the binary was not prepared in advance. For that purpose, a while ago I hacked together pydbattach.

Some solutions:

Blurb answered 20/1, 2024 at 10:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.