title: Python: NameError: free variable 're' referenced before assignment in enclosing scope
body: <p>I have a strange NameError in Python 3.3.1 (win7).</p>
  
  <p>The code:</p>
  
  <pre><code>import re
  
  # ...
  
  # Parse exclude patterns.
  excluded_regexps = set(re.compile(regexp) for regexp in options.exclude_pattern)
  
  # This is line 561:
  excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)
  </code></pre>
  
  <p>The error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "py3createtorrent.py", line 794, in &lt;module&gt;
      sys.exit(main(sys.argv))
    File "py3createtorrent.py", line 561, in main
      excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)
    File "py3createtorrent.py", line 561, in &lt;genexpr&gt;
      excluded_regexps |= set(re.compile(regexp, re.I) for regexp in options.exclude_pattern_ci)
  NameError: free variable 're' referenced before assignment in enclosing scope
  </code></pre>
  
  <p>Note that line 561, where the error occurs, is the <em>second</em> line in the code above. In other words: <code>re</code> is <em>not</em> a free variable. It is simply the regular expression module and it can be referenced perfectly fine in the <em>first</em> line.</p>
  
  <p>It seems to me that the reference to <code>re.I</code> is causing the problem, but I don't see how.</p>