IPython - Enhanced Interactive Python

Global variables undefined in interactive use of embedded ipython shell

Reported by Hans Fangohr on 2009-07-15
18
This bug affects 4 people
Affects Status Importance Assigned to Milestone
IPython
Invalid
Undecided
Unassigned

Bug Description

The error can be reproduced as follows:

1. Start Python, and start embedded ipython session. Following ipython's manual, we do

fangohr@eta:~$ python
Python 2.4.3 (#1, Jun 8 2009, 14:09:06)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from IPython.Shell import IPShellEmbed
>>> ipshell=IPShellEmbed()
>>> ipshell()

Within the just started ipython session, global variables are sometimes not visible. Two examples are:

Example 1:

n [1]: a=1

In [2]: def f(x):
   ...: return a*x
   ...:

In [3]: f(2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/fangohr/<ipython console>

/home/fangohr/<ipython console> in f(x)

NameError: global name 'a' is not defined

Example 2:

In [4]: b=1

In [5]: (lambda :b)()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/fangohr/<ipython console>

/home/fangohr/<ipython console> in <lambda>()

NameError: global name 'b' is not defined

There is no error if "b=1; (lambda :b)()" is put in a script, and this script is executed using ipython's "run" command.

There is no error if "b=1; (lambda :b)()" is executed interactively after having started ipython.

The only way the error seems comes up is if an embedded IPython shell is started from a Python prompt AND the commands are executed interactively at the prompt.

I find the same error when trying ipython-0.9.1 (with python2.4).

The bug was reported by Olivier Klein to the nmag team (http://nmag.soton.ac.uk).

Hans Fangohr (h-fangohr) on 2009-07-15
summary: - Global variables undefined in interactive (REPL) ipython use
+ Global variables undefined in interactive use of embedded ipython shell
Fernando Perez (fdo.perez) wrote :

OK, I can confirm the problem (even on trunk), but it's hard. I am just for now making sure this bug is confirmed so we keep tracking it, but I'm not sure how to fix it.

The issue is that in embedded shells, we try to update the global namespace to use the surrounding scope (that's the point of an embedded shell, to be able to see what's around you). But this then causes python to fail to resolve the ipython interactive namespace when nested things (like local functions) are defined. See the mainloop() method of the embedded shell for details.

I need to think a lot more about how to fix this one, any ideas very much welcome.

Changed in ipython:
status: New → Confirmed
chairmanK (chairmank) wrote :

Me Too. In addition to functions, this bug also appears in generator expressions.

Fernando Perez (fdo.perez) wrote :

I'm closing it here because we've moved all our bug tracking over to github (along with the rest of the code). The bug itself is still open, but we can continue tracking it here (I've added your comment, thanks!):

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

Changed in ipython:
status: Confirmed → Invalid
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Duplicates of this bug

Other bug subscribers