title: Getting "UnboundLocalError: local variable 'connection' referenced before assignment"
body: <p>Im new in python and im getting the above error by trying to assign a connection variable to a function in a different .py file</p>
  
  <p>i've tried to set connection as none but didnt solve my issue</p>
  
  <h1>views.py</h1>
  
  <pre><code>from datetime import datetime
  from flask import render_template, request, redirect, url_for, flash
  from FlaskWebProject1 import app
  
  import json
  import mysql.connector
  from mysql.connector import Error
  import FlaskWebProject1.db 
  
  
  
  @app.route('/')
  @app.route('/users')
  def users():
  
    try: 
      lstUsers = ''
  
      #connect to DB
      connection = db.get_connection()
  
      if connection.is_connected():
          #db_Info = connection.get_server_info()
          #print("Connected to MySQL Server version ", db_Info)
          cursor = connection.cursor(dictionary=True)
          cursor.execute("select * from users;")
          record = cursor.fetchall()
          lstUsers = record       
          #print("Your connected to database: ", record)
    except Error as e:
           print("Error while connecting to MySQL", e) 
    finally:
          if (connection.is_connected()):
              cursor.close()
              connection.close()
              print("MySQL connection is closed")
  
    return render_template('users.html',
      title = 'Users Page', 
      users = lstUsers)
  </code></pre>
  
  <h1>db.py</h1>
  
  <pre><code>_connection = None
  
  def get_connection():
      global _connection
      if not _connection:
          _connection = mysql.connector.connect(user="bla@bla", 
                         password='bla', 
                         host="mysql.database.azure.com", 
                         port=3306, 
                         database='testdb')
          return _connection
  
  # List of stuff accessible to importers of this module. Just in case
  __all__ = [ 'getConnection' ]
  </code></pre>
  
  <blockquote>
    <p>Traceback (most recent call last):   File
    "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\FlaskWebProject1\views.py",
    line 24, in users
        connection = db.get_connection() NameError: name 'db' is not defined</p>
    
    <p>During handling of the above exception, another exception occurred:</p>
    
    <p>Traceback (most recent call last):   File
    "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py",
    line 1982, in wsgi_app
        response = self.full_dispatch_request()   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py",
    line 1614, in full_dispatch_request
        rv = self.handle_user_exception(e)   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py",
    line 1517, in handle_user_exception
        reraise(exc_type, exc_value, tb)   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask_compat.py",
    line 33, in reraise
        raise value   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py",
    line 1612, in full_dispatch_request
        rv = self.dispatch_request()   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\env\lib\site-packages\flask\app.py",
    line 1598, in dispatch_request
        return self.view_functionsrule.endpoint   File "C:\Users\Usuario\source\repos\FlaskWebProject1\FlaskWebProject1\FlaskWebProject1\views.py",
    line 37, in users
        if (connection.is_connected()): UnboundLocalError: local variable 'connection' referenced before assignment
    127.0.0.1 - - [09/Sep/2019 23:56:55] "GET / HTTP/1.1" 500 -
    127.0.0.1 - - [09/Sep/2019 23:56:55] "GET /favicon.ico HTTP/1.1" 404 -</p>
  </blockquote>
  
  <p>I expect to assign the connection variable in views.py file to the get_connection() that returns a connection in db.py and use the connection in order to connect to MySQL server with no issues</p>