Claims not declared, but is... why? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Claims not declared, but is... why? (/thread-17407.html) |
Claims not declared, but is... why? - Ecniv - Apr-09-2019 Hi My class which I've imported in the app.py is complaining that the con variable doesn`t exist. Ok. I get that it thinks the variable doesn`t exist, but it does. before the route is called, it is set up. When you import, does it evaluate the class at that time (if so then that is why.. the variable is only created after the import) from flask import Flask, render_template, flash, redirect, url_for, session, request from flask import send_from_directory, logging import os import pymysql import random from setup import setupADB app = Flask(__name__) app.debug = True app.config.from_object(__name__) app.config['SECRET_KEY'] = 'development key' con = pymysql.connect( host="localhost", user="root", password="thepwd", db="pyadventures", charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor ) #the routes @app.route('/favicon.ico') def rFavicon(): return redirect(url_for('static', filename='favicon.ico'), code=302) @app.route('/') def rIndex(): return render_template("home.html") @app.route('/setup1time') def rSetupOneTime(): r = [] d = setupADB() d.createdb() r = d.dbtables() return 'l' #return render_template('setup.html',r=r) if __name__ == '__main__': app.run()and the class class setupADB: # def __init__(self): #meh def createdb(self): ''' on the command line... mysql -u root -p grant all priveledges on *.* to 'thtename'@'localhost' identifiedby 'pwd'; \q mysql -u username -p create database nameofdatabase use nameofdatabase (or use the mysql manager, create a new schema (database) ) ''' def dbtables(self): r = [] sql = '' #complains on the following line - con should be global from the app.py??? with con.cursor() as cr: try: sql = """CREATE TABLE ... sql """ cr.execute(sql) except Exception as e: r.append("_su -> "+str(e)) try: sql = """CREATE TABLE ... sql """ cr.execute(sql) except Exception as e: r.append("stories -> "+str(e)) return r RE: Claims not declared, but is... why? - Larz60+ - Apr-09-2019 Please show the actual error, complete and unaltered. There is so much information packed in there, and we need it all. RE: Claims not declared, but is... why? - Ecniv - Apr-10-2019
RE: Claims not declared, but is... why? - Larz60+ - Apr-10-2019 in second module: class setupADB: def __init__(self, con): self.con = conthen refer to con in rest of class as self.con also, line 37 of first script d = setupADB(con) RE: Claims not declared, but is... why? - Ecniv - Apr-11-2019 Ah thank you... Thought it would be easy. Can I use the global in the def or would that be bad practice? RE: Claims not declared, but is... why? - Larz60+ - Apr-11-2019 should never use global unless there is no other way. I haven't used a global as far back as I can remember. |