ipy %run noob confusion

Oscar Benjamin oscar.j.benjamin at gmail.com
Fri Oct 4 05:36:21 EDT 2013


On 3 October 2013 18:42,  <jshrager at gmail.com> wrote:
> I have some rather complex code that works perfectly well if I paste it in by hand to ipython, but if I use %run it can't find some of the libraries, but others it can. The confusion seems to have to do with mathplotlib. I get it in stream by:
>
>    %pylab osx
>
> and do a bunch of stuff interactively that works just fine, for example:
>
>   clf()
>
> But I want it to run on a %run, but %pylab is (apparently) not allowed from a %run script, and importing matplotlib explicitly doesn't work...I mean, it imports, but then clf() is only defined in the module, not interactively.

The % commands are ipython magic commands. They are not valid Python
syntax so you can't use them in a Python script. In a Python script
you would use 'from pylab import *' for (roughly) the same effect:

$ ipython
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from pylab import *

In [2]: clf
Out[2]: <function matplotlib.pyplot.clf>

> More confusing, if I do all the setup interactively, and the try to just run my script, again, clf() [etc] don't work (don't appear to exist), even though I can do them interactively.

When you use %run it runs the script "externally". This is basically
the same as typing 'python myscript.py' in the system terminal. In
that case the script needs to import everything it wants to use.

> There seems to be some sort of scoping problem ... or, put more correctly, my problem is that I don't seem to understand the scoping, like, are %run eval'ed in some closed context that doesn't work the same way as ipython interactive? Is there any way to really do what I mean, which is: Please just read in commands from that script (short of getting out and passing my script through stdin to ipython?)

I'm not sure if I understand what you mean but I usually %edit the
script and closing the editor seems to just run the commands as if I
typed them directly in.

If you really want this kind of semi-interactive Matlab-style approach
I suggest having a look at the Spyder IDE.

Personally though I think it's bad to work this way in Python (and in
Matlab) and I discourage my students from doing this. The interactive
interpreter modes are great for testing short snippets of code or
introspecting modules etc. However any real code should go in a real
script. Using %edit for convenience while you write the script is fine
but make sure that what you're creating is a real Python script that
you can run normally with 'python myscript.py'. Spyder is also good
for doing this. Otherwise all of your work, computation and plots are
a mess and it becomes impossible to trace back to exactly how you
produced everything to check your work or to fix it when it becomes
apparent that you've screwed up.


Oscar



More information about the Python-list mailing list