title: how to access global variable within __main__ scope?
body: <p>I'm confused about the namespace and scope of variables in python</p>
  
  <p>Suppose I have a test.py:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  """
  @author: jason
  """
  
  if __name__ == '__main__':
      global strName
      print strName
  </code></pre>
  
  <p>and then, I define a variable named strName and try to access it in the test.py, but it throws an error:</p>
  
  <pre><code>In [9]: strName = "Joe"
  
  In [10]: run test.py hello
  ---------------------------------------------------------------------------  NameError                              Traceback (most recent call last)  C:\Anaconda\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
      195             else:
      196                 filename = fname
  --&gt; 197             exec compile(scripttext, filename, 'exec') in glob, loc
      198     else:
      199         def execfile(fname, *where):
  
  d:\playground\test.py in &lt;module&gt;()
       13         print "hello"
       14         global strName
  ---&gt; 15         print strName
       16 
  
  NameError: global name 'strName' is not defined
  
  In [11]:
  </code></pre>
  
  <p>I was wondering why this happens and is there any way to access strName in test.py?</p>