How to obtain a 'interactive session' of a script?

Bo Peng bpeng at rice.edu
Sun Sep 18 12:08:57 EDT 2005


Thank you for the suggestions and code!

> import code
> 
> SCRIPT = [line.rstrip() for line in open("myscript.py")]
> 
> script = ""
> prompt = ">>>"
> 
> for line in SCRIPT:
>     print prompt, line
>     script = script + line + "\n"
>     co = code.compile_command(script, "<stdin>", "exec")
>     if co:
>         # got a complete statement.  execute it!
>         exec co
>         script = ""
>         prompt = ">>>"
>     else:
>         prompt = "..."
> 

This one fails at function definition.

def fun():
   a=1
   b=2   <--- not included.

Still trying other methods.
Bo



More information about the Python-list mailing list