Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #55428 > unrolled thread

ipy %run noob confusion

Started byjshrager@gmail.com
First post2013-10-03 10:42 -0700
Last post2013-10-04 07:16 -0700
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  ipy %run noob confusion jshrager@gmail.com - 2013-10-03 10:42 -0700
    Re: ipy %run noob confusion Terry Reedy <tjreedy@udel.edu> - 2013-10-03 15:26 -0400
    Re: ipy %run noob confusion Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-03 20:34 +0100
    Re: ipy %run noob confusion Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-10-04 10:36 +0100
    Re: ipy %run noob confusion Jeff Shrager <jshrager@gmail.com> - 2013-10-04 07:16 -0700

#55428 — ipy %run noob confusion

Fromjshrager@gmail.com
Date2013-10-03 10:42 -0700
Subjectipy %run noob confusion
Message-ID<fa461da1-19d9-41e5-8af2-de6c73540023@googlegroups.com>
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. 

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!

[toc] | [next] | [standalone]


#55441

FromTerry Reedy <tjreedy@udel.edu>
Date2013-10-03 15:26 -0400
Message-ID<mailman.695.1380828405.18130.python-list@python.org>
In reply to#55428
On 10/3/2013 1:42 PM, jshrager@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.

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

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#55442

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-10-03 20:34 +0100
Message-ID<mailman.696.1380828899.18130.python-list@python.org>
In reply to#55428
On 03/10/2013 20:26, Terry Reedy wrote:
> On 10/3/2013 1:42 PM, jshrager@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.
>
> 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

[toc] | [prev] | [next] | [standalone]


#55460

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-10-04 10:36 +0100
Message-ID<mailman.712.1380879410.18130.python-list@python.org>
In reply to#55428
On 3 October 2013 18:42,  <jshrager@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

[toc] | [prev] | [next] | [standalone]


#55481

FromJeff Shrager <jshrager@gmail.com>
Date2013-10-04 07:16 -0700
Message-ID<mailman.723.1380896217.18130.python-list@python.org>
In reply to#55428

[Multipart message — attachments visible in raw view] — view raw

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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web