Global variables undefined in interactive use of embedded ipython shell
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=
>>> 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/
/home/fangohr/
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/
/home/fangohr/
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://
summary: |
- Global variables undefined in interactive (REPL) ipython use + Global variables undefined in interactive use of embedded ipython shell |
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.