title: How to create module-wide variables in Python?
body: <p>Is there a way to set up a global variable inside of a module? When I tried to do it the most obvious way as appears below, the Python interpreter said the variable <code>__DBNAME__</code> did not exist.</p>
  
  <pre><code>...
  __DBNAME__ = None
  
  def initDB(name):
      if not __DBNAME__:
          __DBNAME__ = name
      else:
          raise RuntimeError("Database name has already been set.")
  ...
  </code></pre>
  
  <p>And after importing the module in a different file</p>
  
  <pre><code>...
  import mymodule
  mymodule.initDB('mydb.sqlite')
  ...
  </code></pre>
  
  <p>And the traceback was:</p>
  
  <blockquote>
    <p>...
    UnboundLocalError: local variable '<strong>DBNAME</strong>' referenced before assignment
    ...</p>
  </blockquote>
  
  <p>Any ideas? I'm trying to set up a singleton by using a module, as per <a href="https://stackoverflow.com/questions/31875/is-there-a-simple-elegant-way-to-define-singletons-in-python/31887#31887">this fellow's</a> recommendation.</p>