title: psycopg2 across python files
body: <p>I am writing a Python application (console based) that makes use of a PostgreSQL database (via psycopg2) and R (via rpy). It is a large procedure-based application and involves several steps and sometimes repeating of steps and do not always involve all steps.</p>
  
  <p>I have is the following:</p>
  
  <pre><code>main_file.py
  
  modules/__init__.py
  modules/module1.py
  modules/module2.py
  
  functions/__init__.py
  functions/function1.py
  functions/function2.py
  </code></pre>
  
  <p>The init files just states import module1, module2 or function1, function2 depending which init file it is.</p>
  
  <p>The content of the main_file.py looks something like this:</p>
  
  <pre><code>import modules
  from functions import function1
  
  class myClass():
      def my_function(self):
           scripts = [
                      # modules.module1.function, 
                      modules.module2.function, 
                     ]
  
           print "Welcome to the program."
  
           function1.connect()
  
           for i in scripts:
              i
  
           cur.close()
  
           print "End of program"
  
  if __name__ == '__main__':
       myClass().my_function()
  </code></pre>
  
  <p>The reason for the loop is to comment out certain steps if I don't need them. The connect() function I'm trying to call is the psycopg2 connection. It looks like this (inside function1.py file):</p>
  
  <pre><code>import sys
  import psycopg2
  
  def connect():
      try:
          con = psycopg2.connect(database=dbname, user=dbuser)
          cur = con.cursor()
          db = cur.execute
      except psycopg2.DatabaseError, e:
          if con:
               con.rollback()
          print e
          sys.exit
  </code></pre>
  
  <p>In the main_file.py example I'm trying to run module2, which needs to connect to the database, using something like the following:</p>
  
  <pre><code>def function:
      db("SELECT * INTO new_table FROM old_table")
      con.commit()
  </code></pre>
  
  <p>How do I get Python (2.7) to recognise the global names db, cur and con? Thus connecting once-off to the database and keeping the active connection through all steps in the program?</p>