title: Python refactoring: cleaning up imports
body: <p>This might be a newbie-type question (I came to python from PHP5 OO), but I have a few things I am uncertain about the <code>import</code> statement in trying to clean up and refactor some of our Python modules.</p>
  
  <p>Let's say, for example, I have a db connection factory module that imports mysqldb and looks something like this:</p>
  
  <pre><code>import MySQLdb as mysql
  from MySQLdb import cursors
  class ConnectionFactory():
    @staticmethod
    def connect(db_host,db_user,db_pass,db,cursor='DictCursor'):
      connection = mysql.connect(host = db_host,
                     user = db_user,
                     passwd = db_pass,
                     db = db,
                     cursorclass=getattr(mysql.cursors, cursor))
      cur = connection.cursor();
      connection.ping(True)
      return (cur,connection)
  </code></pre>
  
  <p>First, <strong>why is that second import, that explicitly grabs <code>cursors</code> necessary?</strong>  I can't seem to just access mysql.cursors without it.</p>
  
  <p>Also, let's say another module imports this one to get the db connection and then executes queries using it.</p>
  
  <pre><code>import ConnectionFactory
  def runquery(q):
    try:
      cur,connection = ConnectionFactory.connect(db_host,db_user,db_pass,db)
      cur.execute(q)
      res = cur.fetchall()
      return res
    except Exception as e:
      log(str(e))
  </code></pre>
  
  <p>Ideally, that Exception that is being caught should be the MySQLdb.Error exception.  <strong>Does this mean that I need to import MySQLdb here too?</strong>  I assume there's a more elegant way of doing this.  I'm not thinking like a Pythonista, I'm sure.</p>
  
  <p>Lastly, let's say both these modules were imported in another parent for use in a WSGI (I have everything coming in through Werkzeug, which then runs a ton of other methods in other modules)</p>
  
  <p>If the WSGI imports both these modules, I assume the things they import individually aren't in its namespace.  <strong>Is it then bad practice to have the same imports across several modules, if ultimately they are going to be used together in one larger one?</strong></p>
  
  <p><strong>EDIT</strong>: To clarify that third question,
  I've got a special json encoder that's used literally all over the place as the <code>cls</code> arg for <code>json.dumps()</code></p>
  
  <pre><code>from datetime import datetime
  import gnengine
  import json
  
  class SpecialEncoder(json.JSONEncoder):
      def default(self,obj):
      if isinstance(obj,set):
          return list(obj)
      elif isinstance(obj,datetime):
          return obj.isoformat()
      elif isinstance(obj,gnengine.searchresult):
          return obj.jsonify()
  
      return json.JSONEncoder.default(self,obj)
  </code></pre>
  
  <p><code>gnengine</code> is my WSGI, and <code>searchresult</code> is a type found within that module.  I'm importing it here because it has its own json method that my encoder needs to expect.  But this encoder obviously is imported for use in <code>gnengine</code> as well.  They are essentially importing each other.</p>
  
  <p>My PHP background tells me that this is all solved if the <code>searchresult</code> type becomes its own module that represents an object model and then both import that separately.  I'm not sure if this is a big misconception I have though.</p>
  
  <p>Sorry for packaging three questions in one, essentially what I guess is being asked is a clarification about importing in general.  I get the namespacing and why <code>from name import *</code> is a bad idea, I just don't understand the best approach when several packages being used together all have similar dependencies.  Is it expected in python to have identical <code>import</code>s peppered throughout?</p>