[IPython-dev] execute() in IPython widgets

Steve Jackson steve.jackson at icecube.wisc.edu
Wed May 15 11:26:39 EDT 2013


Greetings,

I have an application using the RichIPythonWidget with the QtInProcessKernelManager.  I often need to execute python scripts as if their contents had been typed directly into the widget, i.e., in the __main__ namespace.  The command "execfile('myfile.py')" works correctly for this.  (The magic function "%run myfile.py" does not work, because the file does not execute in the __main__ namespace, but that's okay.)

So far, so good.  Now I want to convince the RichIPythonWidget to behave as if the user had just typed "execfile('myfile.py')" and pressed enter.  Specifically, I need three things to happen:

(1) The widget should open and execute the file...
(2) ...  with the __main__ namespace ...
(3) ... and print any print statements that the file has in it

Here are some things that do not work:

* widget.execute_file('foo.py') -- this behaves the same as the %run magic, providing (1) without (2).

* widget.execute( 'execfile("foo.py")' ) -- this almost works, however, it does not provide (3).  Print messages from foo.py are not echoed to the widget under any combination of arguments to this method.  (In particular, they are suppressed even if interactive=True.)

* widget.kernel_client.execute( 'execfile("myfile.py")' ) -- the file executes, but there are no print messages and exceptions are not raised.  I realize this is a low-level API and that something would need to be done with its return value; I have not explored this further.

Can anyone suggest a workaround?  If this is a bug, I can file a bug report on github, but first I wanted to check whether I am missing something.
~S


More information about the IPython-dev mailing list