Python NameError, global name not defined
<p>I am following <a href="https://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-python-flask-and-mysql--cms-22972" rel="nofollow">this</a> tutorial to create a Python 2.7 and Flask web app with a MySQL database.</p>

<p>Why am I getting this error on <code>_password</code>?</p>

<p><code>app.py</code></p>

<pre><code>from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash

# create the app
app = Flask(__name__)

@app.route("/")
def main():
    return render_template("index.html")

@app.route("/showSignUp")
def showSignUp():
    return render_template("signup.html")

@app.route("/signUp", methods=["POST"])
def signUp():
    # read the values input by the user
    global _name, _email, _password
    _name = request.form["inputName"]
    _email = request.form["inputEmail"]
    _password = request.form["inputPassword"]

    # validate inputs
    if _name and _email and _password:
        return json.dumps({"html":"&lt;span&gt;fields are good&lt;/span&gt;"})
    else:
        return json.dumps({"html":"&lt;span&gt;enter required fields&lt;/span&gt;"})

# MySQL configurations
app.config["MYSQL_DATABASE_USER"] = "root"
app.config["MYSQL_DATABASE_PASSWORD"] = ""
app.config["MYSQL_DATABASE_DB"] = "NotesList"
app.config["MYSQL_DATABASE_HOST"] = "localhost"
MySQL().init_app(app)

def connectDB():
    conn = mysql.connect()
    cursor = conn.cursor()

_hashed_password = generate_password_hash(_password)

cursor.callproc("sp_createUser", (_name, _email, _hashed_password))

def check():
    data = cursor.fetchall()
    if len(data) is 0:
        conn.commit()
        return json.dumps({"message":"user created"})
    else:
        return json.dumps({"error":str(data[0])})

app.debug = True
if __name__ == "__main__":
    app.run()
</code></pre>

<p>The reason is that the app can't be instantiated because you're calling upon names that haven't been created yet. It's almost NEVER a good idea to use <code>global</code> to move variables into the global scope. Don't do that.</p>

<p>Even if you decided you don't care about best-practices, the variables won't get into the global scope until the function is actually called, which never happens before those variables are being used in your code.</p>

<p>You should make your db connection before the request. You'll probably want to utilize the <a href="http://flask.pocoo.org/docs/0.11/api/#flask.Flask.before_request" rel="nofollow">before_request</a> decorator to do this, storing the connection in <code>g</code> -- see the <a href="http://flask.pocoo.org/docs/0.11/tutorial/dbcon/" rel="nofollow">database connection</a> portion of the Flaskr tutorial for more info.</p>

<p>Then put the parts of the code that <strong>work with the database INSIDE of the route function</strong>. The way your code is written write now, you're executing those lines EVERY time your app gets instantiated, not just when someone uses the view <code>/signup</code></p>

python mysql flask