ipy %run noob confusion

J

jshrager

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 froma %run script, and importing matplotlib explicitly doesn't work...I mean, it imports, but then clf() is only defined in the module, not interactively..

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.

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?)

Thanks!
 
T

Terry Reedy

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.

Ipython is a separate product built on top of Python. If no answer here,
look for an ipython-specific list or discussion group.
 
M

Mark Lawrence

Ipython is a separate product built on top of Python. If no answer here,
look for an ipython-specific list or discussion group.

Such as news.gmane.org/gmane.comp.python.ipython.user

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
O

Oscar Benjamin

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]: said:
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 %runeval'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
 
J

Jeff Shrager

Thank you. This is extremely helpful. The key that I was missing is that
it's running them outside of the ipy context. I also discovered that if you
call the script .ipy instead of .py, it actually does more or less what I
was expecting -- that is, it allows magic commands, and I got the thing
working as desired through this means.

I do appreciate the concern that my script should be "real" python, and I
would aim for that eventually, but while I'm experimenting, it's useful to
be able to have the interactive and script work the same way. I'm happy to
"real-ify" it later.

BTW, I generally work the opposite way than %edit: I code in emacs, and
have a shell that's running ipy. Actually ipy is bad at this because it
sends all sorts of terminal crap that I can probably get rid of somehow,
but working this way has many advantages, not the least of which is that
you don't have to fire up editors over and over (my editor context is
primary), and I get an automatic log of my whole session in the shell
buffer. This mode has served me well using regular python, because I didn't
have the .ipy v. .py problem, but when I started into ipyton, things no
longer worked. But your guidance (and others) have help deconfuse me on
this. Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top