question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Variable not recognized in nested or filtered list comprehension under IPython.embed()

See original GitHub issue

If I run IPython.embed() and then try to execute the following code from the command line, I get a NameError. Variables defined in the local namespace seem to become invisible when there is a nested list comprehension. They are visible for a single-level list comprehension, but I get a NameError if I refer to a variable from a second or deeper level.

This does not happen with a normal IPython shell or when I use IPython.start_ipython().

Here is the code to reproduce the error.

In [1]: x = [1, 2, 3]                                                                                                                                                                            
In [2]: [i for i in x]                                                                                                                                                                           
Out[2]: [1, 2, 3]  # fine

In [3]: [i for i in x for j in x]                                                                                                                                                                
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-ebe839e86c71> in <module>
----> 1 [i for i in x for j in x]

<ipython-input-2-ebe839e86c71> in <listcomp>(.0)
----> 1 [i for i in x for j in x]

NameError: name 'x' is not defined

In [4]: y = [4, 5, 6]                                                                                                                                                                            
In [5]: [i for i in x for j in y]                                                                                                                                                                
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-ba9a21c5a38c> in <module>
----> 1 [i for i in x for j in y]

<ipython-input-5-ba9a21c5a38c> in <listcomp>(.0)
----> 1 [i for i in x for j in y]

NameError: name 'y' is not defined

This is in IPython 7.9.0 with Python 3.7.3.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tciescommented, Feb 27, 2022

Same, I love IPython, I love IPython.embed(), and this issue makes it significantly less practical to work with 😢 .

2reactions
congvmitcommented, Feb 18, 2022

I am facing this problems while debugging my code. Any updates?

Read more comments on GitHub >

github_iconTop Results From Across the Web

nested loop list comprehension in python - Stack Overflow
nested loop list comprehension in python ; can't recognize variable in outer ... The error shows NameError: global name 'a' is not defined....
Read more >
IO tools (text, CSV, HDF5, …) — pandas 1.5.2 documentation
If a column or index contains an unparsable date, the entire column or index will be returned unaltered as an object data type....
Read more >
self' not defined when using a list comprehension
when I run it I get 'NameError: name 'self' is not defined' on the last line. I printed the locals() before and during...
Read more >
List Comprehension in Python — A Helpful Illustrated Guide
List comprehension is a compact way of creating lists. ... Use any variable in your expression that you have defined in the context...
Read more >
TypeError: list indices must be integers or slices, not str
Usually, the most straightforward method for solving this problem is to convert any relevant values to integer type using the int() function. Something...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found