[IPython-user] embedded shell forgets global imports

Fernando Perez Fernando.Perez at colorado.edu
Wed Sep 21 01:04:43 CDT 2005


Yaroslav Bulatov wrote:
> When I drop into embedded IPython from within function, I can't access
> my global imports for that script file. Is this intended behaviour? Is
> there a way to make those imports accessible to embedded shell?
> 
> deleteme.py
> --------------
> import pylab
> 
> from IPython.Shell import IPShellEmbed
> ipshell = IPShellEmbed()
> 
> def a():
>   ipshell()
> 
> a()
> 
> 
> In [2]: %run deleteme.py
> In [1]: pylab
> ---------------------------------------------------------------------------
> exceptions.NameError                                 Traceback (most recent call
>  last)
> 
> c:\projects\scores\current\<console>
> 
> NameError: name 'pylab' is not defined

What you are trying to do is to recursively embed one ipython inside another 
(you used %run, from ipython, to run a file which itself loads an ipython 
instance).  While I've tried to make this work, it's (obviously) not very 
reliable nor tested.  Each ipython tries to emulate a 'standalone' python 
environment, when in reality they are all living inside the CPython 
interpreter.  This is bound to cause problems, and I've never sorted out a 
full solution.

The typical (and fairly reliable) way to use embedded ipythons is from the 
plain comand line:

[test]> cat embed-bug.py
import pylab

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()

def a():
     ipshell()

a()
[test]> python embed-bug.py


In [1]: pylab
Out[1]: <module 'pylab' from '/usr/lib/python2.3/site-packages/pylab.pyc'>


This works just fine.

If anyone manages to disentangle the namespace hairball that recursively 
embedded ipythons causes, I'll gladly take the patches :)

> PS: is there a way to search the mailing list archives?

I just use google, but the gmane gateway for each list (posted on the ipython 
page) has a search box you can use if you prefer.

Cheers,

f




More information about the IPython-user mailing list