interactive python shell

Quinn Dunkan quinn at vomit.ugcs.caltech.edu
Mon Apr 1 03:07:33 EST 2002


On Wed, 27 Mar 2002 13:33:37 -0500, ian reinhart geiser <geiseri at yahoo.com>
wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Greetings
>	I have an python application and I would like to provide a "console" in the 
>application where users can have an interactive session with python that has 
>all of the applications enviroment.  This is for debugging the system in real 
>time.
>
>	Is there an easy way to do this?  All it needs to do is execute commands and 
>return data.  I am using PyQt for my GUI, so it would be cool to fit into 
>there, but I can get away with a input line and a display view if that is 
>easier.  This is not for production, only to make a more useful debug tool.

Other people have pointed out some modules, but when I want to talk to python
for debugging, I do the following.  It's simple and basically works.
You can pass eval and exec namespaces if you want.

def repl():
    try:
        while 1:
            c = raw_input(sys.ps1)
            if not c:
                continue
            if c[-1] == ':':
                while 1:
                    s = raw_input(sys.ps2)
                    if not s:
                        c = c + '\n'
                        break
                    c = c + '\n' + s
            try:
                print repr(eval(c))
            except SyntaxError:
                try:
                    exec c
                except:
                    traceback.print_exc()
            except:
                traceback.print_exc()
    except:
        print

I don't know about PyQt, but it should be easy to adapt to whatever it likes...
replace raw_input with reading from a stream in a seperate thread if it
does that or take out the line reading loop and have it process incomplete
blocks if it likes callbacks.



More information about the Python-list mailing list