[IPython-user] reducing function visibility in pylab

Fernando Perez fperez.net at gmail.com
Mon Jan 1 15:12:00 CST 2007


On 12/31/06, belinda thom <bthom at cs.hmc.edu> wrote:
> Hi,
>
> Is there a way to run w/the pylab flag but NOT have a pile of
> functions (e.g. plot, axis, ...) show up in the namespace?
>
> What I'd really like is to be able to start in pylab mode (so the
> threading stuff is set up), and then have two modules to show up (in
> such a way that %who can print their names). I'd also like for dir()
> to return very little (not the slew of functions pylab creates).
>
> I get half of what I want by doing:
>
>    ipython
>    <snip>
>    In [3]: import numpy as N
>    In [5]: import pylab as P
>    In [6]: who
>    N       P
>
> But then I've not started in the pylab mode needed for interactive
> drawing.

Unfortunately no.  The mpl initialization code is hard-wired in
IPython/Shell.py, around line 448:

        # Build a user namespace initialized with matplotlib/matlab features.
        user_ns = IPython.ipapi.make_user_ns(user_ns)

        exec ("import matplotlib\n"
              "import matplotlib.pylab as pylab\n"
              "from matplotlib.pylab import *") in user_ns

In principle this could be made configurable via an rc flag or by
storing this init string into the instance and allowing subclasses to
reset it, but it hasn't really been done.

As far as dir(), it's a builtin whose output we don't control, it will
always return everything visible.  So only a modification of the above
to NOT use 'import *' would change that result.  Alternatively, you
could override dir() with a custom version which calls the builtin and
filters out things you don't want to be visible, that's in essence
what %who does.

Regards,

f


More information about the IPython-user mailing list