Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The first one with 'import code; code.interact(local=locals())' is priceless.

Is there a way to attach a python shell to a running process? I saw something similar for ruby but could not find a python equivalent.



People that find code.interact useful should check out IPython* (which is an interactive Python shell with auto completion and a lot of other features).

Here is how I launch a shell for my projects (it tries to use IPython, if it isn't working it uses code.interact): http://paste.plurk.com/show/17110/

* http://ipython.scipy.org/moin/


IPython is great and I used to be a huge fan before I found Spyder (formerly Pydee.) It is somewhat a matter of taste, but when working interactively I think Spyder is one of the best shells. It is available at http://code.google.com/p/spyderlib/


Upvote for iPython, it quickened my learning of the language 10 fold. That and slime.vim is a match made in heaven!


Not that I know of. In the past I've allowed my process (which was essentially a HTTP-based app server) to evaluate commands from a secure source, then built a Python interpreter that sends what you type via to HTTP to it -- so you get a pseudo shell. At another point I've set up a signal, e.g. SIGUSR2 to essentially do the code.interact() thing like in the article -- so when you need to you can kill -USR2 the process and get into a shell. The first method lets you keep running the server while the second one freezes it while you debug it.

There are also macros for gdb floating around -- you can attach the gdb (C etc.) debugger to a python process and then examine the stack through some gdb macros, but the interactivity is limited (unless perhaps someone has built it up more). That lets you break in at any moment. See http://wiki.python.org/moin/DebuggingWithGdb

In many cases where a production process is doing mysterious things, using strace or perhaps ltrace on it can give a good hint to what it is doing, together with lsof to see what files it's reading/writing.


You might be interested in this package I wrote for one of my projects:

http://github.com/cool-RR/PythonTurtle/

Check out the directory "/src/shelltoprocess".

It's a package for connecting a GUI shell, running on wxPython, to a Python process spawned by the multiprocessing package. It's documented and MIT-licensed.


Twisted's manhole does exactly this. SSH into a process! http://www.devshed.com/c/a/Python/SSH-with-Twisted/3/


WinPdb well let you do this. It lets you start a process, and then attach a graphical debugger to it over TCP. Then you can let the process run, break and examine whenever you want, set break points, conditionals, etc. Great for debugging things like FCGIs, FUSE plugings, etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: