title: Visibility of global variables in imported modules
body: <p>I've run into a bit of a wall importing modules in a Python script. I'll do my best to describe the error, why I run into it, and why I'm tying this particular approach to solve my problem (which I will describe in a second):</p>
  
  <p>Let's suppose I have a module in which I've defined some utility functions/classes, which refer to entities defined in the namespace into which this auxiliary module will be imported (let "a" be such an entity):</p>
  
  <p>module1:</p>
  
  <pre><code>def f():
      print a
  </code></pre>
  
  <p>And then I have the main program, where "a" is defined, into which I want to import those utilities:</p>
  
  <pre><code>import module1
  a=3
  module1.f()
  </code></pre>
  
  <p>Executing the program will trigger the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "Z:\Python\main.py", line 10, in &lt;module&gt;
      module1.f()
    File "Z:\Python\module1.py", line 3, in f
      print a
  NameError: global name 'a' is not defined
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/15890014/python-namespaces-with-module-imports">Similar questions have been asked</a> in the past (two days ago, d'uh) and several solutions have been suggested, however I don't really think these fit my requirements. <strong>Here's my particular context:</strong></p>
  
  <p>I'm trying to make a Python program which connects to a MySQL database server and displays/modifies data with a GUI. For cleanliness sake, I've defined the bunch of auxiliary/utility MySQL-related functions in a separate file. However they all have a common variable, which I had originally defined <em>inside</em> the utilities module, and which is the <em>cursor</em> object from MySQLdb module.
  I later realised that the <em>cursor</em> object (which is used to communicate with the db server) should be defined in the main module, <strong>so that both the main module and anything that is imported into it can access that object.</strong></p>
  
  <p>End result would be something like this:</p>
  
  <p>utilities_module.py:</p>
  
  <pre><code>def utility_1(args):
      code which references a variable named "cur"
  def utility_n(args):
      etcetera
  </code></pre>
  
  <p>And my main module:</p>
  
  <p>program.py:</p>
  
  <pre><code>import MySQLdb, Tkinter
  db=MySQLdb.connect(#blahblah) ; cur=db.cursor()  #cur is defined!
  from utilities_module import *
  </code></pre>
  
  <p>And then, as soon as I try to call any of the utilities functions, it triggers the aforementioned "global name not defined" error.</p>
  
  <p>A particular suggestion was to have a "from program import cur" statement in the utilities file, such as this:</p>
  
  <p>utilities_module.py:</p>
  
  <pre><code>from program import cur
  #rest of function definitions
  </code></pre>
  
  <p>program.py:</p>
  
  <pre><code>import Tkinter, MySQLdb
  db=MySQLdb.connect(#blahblah) ; cur=db.cursor()  #cur is defined!
  from utilities_module import *
  </code></pre>
  
  <p>But that's cyclic import or something like that and, bottom line, it crashes too. So my question is:</p>
  
  <p><strong>How in hell can I make the "cur" object, defined in the main module, visible to those auxiliary functions which are imported into it?</strong></p>
  
  <p>Thanks for your time and my deepest apologies if the solution has been posted elsewhere. I just can't find the answer myself and I've got no more tricks in my book.</p>