python - What is the correct way to solve this circular import error with a Flask blueprint? -


i had problem circular import, moved blueprint import below app definition. however, i'm still having import error.

traceback (most recent call last):   file "/applications/pycharm ce.app/contents/helpers/pydev/pydevd.py", line 2217, in <module>     globals = debugger.run(setup['file'], none, none)   file "/applications/pycharm ce.app/contents/helpers/pydev/pydevd.py", line 1643, in run     pydev_imports.execfile(file, globals, locals)  # execute script   file "/users/benjamin/documents/projects/website/server/app/app.py", line 15, in <module>     views import site   file "/users/benjamin/documents/projects/website/server/app/views.py", line 2, in <module>     models import user   file "/users/benjamin/documents/projects/website/server/app/models.py", line 3, in <module>     database_setup import db   file "/users/benjamin/documents/projects/website/server/app/database_setup.py", line 1, in <module>     app import app   file "/users/benjamin/documents/projects/website/server/app/app.py", line 15, in <module>     views import site importerror: cannot import name site 

if move blueprint import , registration if __name__ == '__main__':, problem goes away, i'm not sure if idea.

if __name__ == '__main__':     views import site     app.register_blueprint(site)     app.run() 

is right way solve problem, or there solution?


original app.py without __main__ "fix":

from flask import flask  app = flask(__name__)  views import site app.register_blueprint(site)  if __name__ == '__main__':     app.debug = true     app.run() 

views.py:

from flask import blueprint, render_template  site = blueprint('site', __name__, template_folder='templates', static_folder='static')  @site.route('/', methods=['get', 'post']) def index():     return render_template('index.html') 

database_setup.py:

from app import app flask_mongoengine import mongoengine  app.config['mongodb_settings'] = {'db': 'mst_website'}     db = mongoengine(app) 

models.py:

from database_setup import db  class user(db.document):     # ... 

my file structure is:

/server   |-- requirements.txt   |-- env/ (virtual environment)   |-- app/ (my main app folder)        |-- static/        |-- templates/        |-- __init__.py        |-- app.py        |-- database_setup.py        |-- models.py        |-- views.py 

you have circular import in code. based on traceback:

  1. app.py from views import site
  2. views.py from models import user
  3. models.py from database_setup import db
  4. database_setup.py from app import app
  5. app.py from views import site

based on these order of events, app.py you've posted isn't 1 that's causing problem. right now, app hasn't been defined before views imported, when further down chain tries app, it's not available yet.

you need restructure project depends on app imported after app defined. question, seems think did, perhaps there's still import written above app missed.


probably unrelated, using "relative" imports, discouraged. rather doing from views import site, etc., should absolute path: from app.views import site, or relative path: from .views import site.


to answer initial question of "is using __main__ import blueprints idea?", not. problem __main__ guard only executed when run module directly. when go deploy using real app server such uwsgi or gunicorn, none of blueprints imported or registered.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -