title: iPython debugger raises `NameError: name ... is not defined`
body: <p>I can't understand the following exception that is raised in this Python debugger session:</p>
  
  <pre><code>(Pdb) p [move for move in move_values if move[0] == max_value]
  *** NameError: name 'max_value' is not defined
  (Pdb) [move for move in move_values]
  [(0.5, (0, 0)), (0.5, (0, 1)), (0.5, (0, 2)), (0.5, (1, 0)), (0.5, (1, 1)), (0.5, (1, 2)), (0.5, (2, 0)), (0.5, (2, 1)), (0.5, (2, 2))]
  (Pdb) max_value
  0.5
  (Pdb) (0.5, (0, 2))[0] == max_value
  True
  (Pdb) [move for move in move_values if move[0] == 0.5]
  [(0.5, (0, 0)), (0.5, (0, 1)), (0.5, (0, 2)), (0.5, (1, 0)), (0.5, (1, 1)), (0.5, (1, 2)), (0.5, (2, 0)), (0.5, (2, 1)), (0.5, (2, 2))]
  (Pdb) [move for move in move_values if move[0] == max_value]
  *** NameError: name 'max_value' is not defined
  </code></pre>
  
  <p>Why is it sometimes telling me <code>max_value</code> is not defined and other times not?</p>
  
  <p>Incidentally, this is the code immediately prior to the debugger starting:</p>
  
  <pre><code>max_value = max(move_values)[0]
  best_moves = [move for move in move_values if move[0] == max_value]
  import pdb; pdb.set_trace()
  </code></pre>
  
  <p>I am using Python 3.6 running in PyCharm.</p>
  
  <p><strong>AMENDED UPDATE:</strong></p>
  
  <p>After more testing it appears that local variables are not visible within list comprehensions within a <code>pdb</code> session when I do the following from an iPython REPL or in PyCharm:</p>
  
  <pre><code>$ ipython
  Python 3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
  Type 'copyright', 'credits' or 'license' for more information
  IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.
  
  In [1]: import pdb; pdb.set_trace()
  --Call--
  &gt; /Users/billtubbs/anaconda/envs/py36/lib/python3.6/site-packages/IPython/core/displayhook.py(247)__call__()
  -&gt; def __call__(self, result=None):
  (Pdb) x = 1; [x for i in range(3)]
  *** NameError: name 'x' is not defined
  </code></pre>
  
  <p>But in a regular Python REPL it works:</p>
  
  <pre><code>$ python
  Python 3.6.5 | packaged by conda-forge | (default, Apr  6 2018, 13:44:09) 
  [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import pdb; pdb.set_trace()
  --Return--
  &gt; &lt;stdin&gt;(1)&lt;module&gt;()-&gt;None
  (Pdb) x = 1; [x for i in range(3)]
  [1, 1, 1]
  </code></pre>
  
  <p>I tested above with versions 3.4, 3.5, 3.6 so it does not appear to be version dependent.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Please note, the above test ('AMENDED UPDATE') is problematic because it uses <code>import pdb; pdb.set_trace()</code> in the interactive REPL.  </p>
  
  <p>Also, the original problem is not limited to iPython.</p>
  
  <p>See <a href="https://stackoverflow.com/a/54338006/1609514">answer by user2357112</a> below for a comprehensive explanation of what is going on here.</p>
  
  <p>Sorry if I caused any confusion!</p>