Reputation: 346
I am trying to build a small web app for my analyst team. They just need a repository with which they can add and view the queries that they have written.
Thus far, I have been unable to implement my app because I can't import functions across the views/classes. Can someone help me figure out what my issue is?
I receive this error:
File "C:\Users\Intern2\PycharmProjects\queries-Final2\queries_Final2.py", line 12, in <module>
from search_queries import SearchQueries
ImportError: cannot import name SearchQueries
I think that issue has to do with the fact that I try to import each file into the other file. However, I don't know of any other way to do this.
Here is my code:
queries_Final2.py (main application file)
from __future__ import with_statement
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort,\
render_template, flash, _app_ctx_stack
import flask
import settings
import functools
# Views
from main import Main
from login import Login
from search_queries import SearchQueries
from add_queries import AddQueries
from edit_queries import EditQueries
DATABASE = 'C:\\Users\\Intern2\\PycharmProjects\\queries-Final2\\queriesDB.db'
app = flask.Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
app.secret_key = settings.secret_key
app.add_url_rule('/search-queries/',
view_func=SearchQueries.as_view('search-queries'),
methods=["GET", "POST"])
app.add_url_rule('/add-queries/',
view_func=AddQueries.as_view('add-queries'),
methods=["GET", "POST"])
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit()
def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
top = _app_ctx_stack.top
if not hasattr(top, 'sqlite_db'):
top.sqlite_db = sqlite3.connect(app.config['DATABASE'])
return top.sqlite_db
Display Queries view/class
from __future__ import with_statement
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort,\
render_template, flash, _app_ctx_stack
import flask
import settings
import functools
from queries_Final2 import get_db
import utils
class SearchQueries(flask.views.MethodView):
@utils.login_required
def get(self):
db = get_db()
cur = db.execute('select title, columns, query, notes, tags from entries order by id desc')
entries = [dict(title=row[0], columns=row[1], query=row[2],notes=row[3],tags=row[4]) for row in cur.fetchall()]
return render_template('search-queries.html', entries=entries)
Add queries class/view
from __future__ import with_statement
from flask import Flask, request, session, g, redirect, url_for, abort,\
render_template, flash, _app_ctx_stack
import flask
import functools
import utils
from queries_Final2 import get_db
class AddQueries(flask.views.MethodView):
@utils.login_required
def get(self):
return flask.render_template('add-queries.html')
def post(self):
db = get_db()
db.execute('insert into entries (title, columns, query, notes, tags) values (?, ?, ?, ?, ?)',
[request.form['title'],
request.form['columns'],
request.form['query'],
request.form['notes'],
request.form['tags']
])
db.commit()
flash('New entry was successfully posted')
return flask.render_template('search-queries.html')
Any ideas would be greatly appreciated. Thanks in advance.
Upvotes: 2
Views: 2722
Reputation: 57198
You have a circular import triggered by from queries_Final2 import get_db
.
You're doing this, effectively (pseudo-code):
# Inside module A
import x from module B
-> goes to module B to get x
-> module B begins loading its imports (happens on first import)
-> module B imports y from module A
-> module B fails to import y from module A because module A is still being defined
Over time, you'll learn how to abstract your tools in a manner that will avoid this problem, but in the meantime, you can fix it by simply moving the lines from queries_Final2 import get_db
to inside your get
and post
functions, instead of leaving them at the module level.
Upvotes: 3