title: How is the __name__ variable in a Python module defined?
body: <p>I'm aware of <a href="http://docs.python.org/2/tutorial/modules.html#executing-modules-as-scripts">the standard example</a>: if you execute a module directly then it's <code>__name__</code> global variable is defined as <code>"__main__"</code>. However, nowhere in the documentation can I find a precise description of how <code>__name__</code> is defined in the general case. The <a href="http://docs.python.org/2/tutorial/modules.html">module documentation</a> says...</p>
  
  <blockquote>
    <p>Within a module, the module's name (as a string) is available as the value of the global variable <code>__name__</code>. </p>
  </blockquote>
  
  <p>...but what does it mean by "the module's name"? Is it just the name of the module (the filename with <code>.py</code> removed), or does it include the fully-qualified package name as well?</p>
  
  <p>How is the value of the <code>__name__</code> variable in a Python module determined? For bonus points, indicate precisely where in the Python source code this operation is performed.</p>