title: Python compile function, NameError: name 'n' is not defined
body: <p>I am a python leaner, right now I using <code>compile</code> function to get a variable from <code>input</code></p>
  <pre><code>def main():
      c = compile(&quot;n = int(input('&gt;&gt;&gt;'))&quot;, &quot;&quot;, &quot;single&quot;)
      exec(c)
      print(n) # NameError: name 'n' is not defined
  
  
  
  if __name__ == '__main__':
      main()
      c = compile(&quot;n = int(input('&gt;&gt;&gt;'))&quot;, &quot;&quot;, &quot;single&quot;)
      exec(c)
      print(n) # good, no error
  </code></pre>
  <p>same code has different effects in <code>main()</code> or under <code>if __name__ == '__main__':</code></p>