[IPython-User] [IPython-user] using magic command inside the loop

Paul Ivanov pivanov314@gmail....
Wed Dec 29 03:16:39 CST 2010


unit, on 2010-12-28 12:52,  wrote:
> 
> Hi all,
> 
> So, I want to use %run inside a for loop in my script.py to call for another
> script_1.py. 
> Something like this:
> 
> #beginning of script.py
> import IPython
> ip = IPython.ipapi.get()
> for i in range(0,5):
>           ip.magic('%run script_1.py')
> #end of the script.py
> 
> However, when I execute script.py in my IPython shell, I get: "NameError:
> name 'ip' is not defined"
> My question is this: how come ip is not defined inside the for scope is I
> just defined it before entering the loop?

The reason is that when you use IPython's run magic, it pulls the
wool over your script's eyes and creates a fantasy world which
makes it believe that it is running in its own empty python
namespace. So anything you define in IPython stays behind the
curtain if you use the run magic. If you want to *expose* the
things you define in the interactive session to the scripts
you're calling, pass the -i flag to run. From the docs:

	-i: run the file in IPython's namespace instead of an empty one.
	This is useful if you are experimenting with code written in a
	text editor which depends on variables defined interactively.

In the process of creating the illusion, it seems some things can
get hidden away that shouldn't be. As a workaround, try adding
the -i flag to run

In [1]: run -i script

to see if that fixes the issue, and possibly adding the -i to the
ip.magic parameter, if script_1.py also refers to ip.

IPython Developers: I can confirm that with an empty script_1.py,
calling script.py fails with a NameError *after* the first call
to script_1.py is made. 


Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
Type "copyright", "credits" or "license" for more information.

IPython 0.10.2.git -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ??
prints more.

In [1]: run script
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/home/pi/code/ipython/script_1.py in <module>()
      3 ip = IPython.ipapi.get()
      4 for i in range(0,5):
----> 5     ip.magic('%run script_1.py')
      6 #end of the script.py
      7

NameError: name 'ip' is not defined
WARNING: Failure executing file: <script.py>

In [2]: debug
> /home/pi/code/ipython/script.py(5)<module>()
      4 for i in range(0,5):
----> 5     ip.magic('%run script_1.py')
      6 #end of the script.py


ipdb> print i # <-- NB!!
1
ipdb> l
      1 #beginning of script.py
      2 import IPython
      3 ip = IPython.ipapi.get()
      4 for i in range(0,5):
----> 5     ip.magic('%run script_1.py')
      6 #end of the script.py
ipdb>


This doesn't seem restricted to IPython modules, either. If I
modify script.py to import time (or any other module) and print
it at the beginning of each iteration, I get a NameError for it
as well.

In [1]: run script
<module 'time' (built-in)>
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/home/pi/code/ipython/script_1.py in <module>()
      4 ip = IPython.ipapi.get()
      5 for i in range(0,5):
----> 6     print time
      7     ip.magic('%run script_1.py')
      8 #end of the script.py


NameError: name 'time' is not defined
WARNING: Failure executing file: <script.py>

In [2]: debug
> /home/pi/code/ipython/script.py(6)<module>()
      5 for i in range(0,5):
----> 6     print time
      7     ip.magic('%run script_1.py')

ipdb> l
      1 #beginning of script.py

      2 import IPython
      3 import time
      4 ip = IPython.ipapi.get()
      5 for i in range(0,5):
----> 6     print time
      7     ip.magic('%run script_1.py')
      8 #end of the script.py


ipdb> print i
1


This seems to be a property of calling "run" magic from another
"run" magic - and I'm having deja vu about running into the issue
before, so it may well have been reported. Nevertheless, I've
opened issue 238 in case other run into it.

https://github.com/ipython/ipython/issues/issue/238

-- 
Paul Ivanov
314 address only used for lists,  off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
Url : http://mail.scipy.org/pipermail/ipython-user/attachments/20101229/a57d88bc/attachment.bin 


More information about the IPython-User mailing list