"global name is not defined" from `embed` with nested list comprehension #10695

Open
bluenote10 opened this Issue Jul 11, 2017 · 2 comments

Comments

Projects
None yet
2 participants

The following code is valid Python and works fine from a stand-alone IPython shell, but fails from within an IPython.embed:

$ python -c "import IPython; IPython.embed()"

In [1]: l = [1, 2, 3]

In [2]: d = {key: [key*x for x in l] for key in ["A", "B"]}
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-bc78f2357f34> in <module>()
----> 1 d = {key: [key*x for x in l] for key in ["A", "B"]}

<ipython-input-2-bc78f2357f34> in <dictcomp>((key,))
----> 1 d = {key: [key*x for x in l] for key in ["A", "B"]}

NameError: global name 'l' is not defined
{'commit_hash': u'1149d1700',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/home/PHI-TPS/fkeller/.virtualenvs/ipython/lib/python2.7/site-packages/IPython',
 'ipython_version': '5.4.1',
 'os_name': 'posix',
 'platform': 'Linux-3.13.0-100-generic-x86_64-with-Ubuntu-12.04-precise',
 'sys_executable': '/home/PHI-TPS/fkeller/.virtualenvs/ipython/bin/python',
 'sys_platform': 'linux2',
 'sys_version': '2.7.3 (default, Oct 26 2016, 21:01:49) \n[GCC 4.6.3]'}

Another search has brought up #8918, not clear why it's closed though.

Owner

takluyver commented Jul 13, 2017

See also #62. There's a limitation of executing Python code in a local namespace, which we haven't found a good way to work around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment