[IPython-User] auto-execute code on Qt console startup

Fernando Perez fperez.net@gmail....
Thu Nov 24 11:11:54 CST 2011

Hi Martin,

On Thu, May 26, 2011 at 3:25 PM, Martin Spacek <ipython@mspacek.mm.st> wrote:
> I realize the ipython Qt widget doesn't yet handle ipython's config yet, so the
> user's ipython_config.py is ignored for now. Until that's implemented, I'd like
> to nevertheless have a line or two executed every time I start
> ipython-qtconsole, or better yet, every time I init an IPythonWidget, say
> something like:
> line = "import numpy as np"
> Global.exec_lines would normally be the place to specify this in the config.
> I've tried calling self.execute(line) in various places. Putting it at the end
> of IPythonWidget.__init__(), it chokes because the connection to the kernel
> isn't ready yet. Putting widget.execute(line) just before app.exec_() in
> ipythonqt.py works, but somehow interrupts the normal printing of the IPython
> display header. Also, if I call it more than once, I get a "RuntimeError: Cannot
> change input buffer during execution."
> Is there some ideal place to put something like this? Maybe some method that's
> automatically called after everything has init'd and the connection to the
> kernel is complete?

sorry you never got a reply to this... We're so swamped with pull
requests, and we're such a small team, that we do fall behind on the
user list more than we'd like.

The qt console now does honor the config machinery, so if you fill out
in the config file the exec_lines variable with a list of strings you
want executed, e.g.:

# lines of code to run at IPython startup.
c.InteractiveShellApp.exec_lines = ["""
import sys, os, math
ip = get_ipython()
#%load_ext storemagic



More information about the IPython-User mailing list