user3151858
user3151858

Reputation: 820

AttributeError: module 'flask.app' has no attribute 'route'

I have autocomplete function as a separate file - autocomplete.py. The autocomplete is being imported into init.py as follows:

from xyzapp.autocomplete import autocomplete

The line that throws error in autocomplete.py is the following:

@app.route('/autocomplete',methods=['GET'])

The structure of the application is using blueprints and looks as follows:

appcontainer
     |
     |--run.py
     |      
     |
     |--xyzapp
          |--__init__.py
          |--autocomplete.py
          |--blueprint_folder_1
          |--blueprint_folder_2
          |--static

The whole error message looks like this:

 @app.route('/autocomplete',methods=['GET'])
 AttributeError: module 'flask.app' has no attribute 'route' 

Any ideas what I am doing wrong?

UPDATE:

The autocomplete.py looks as follows:

from flask import Flask, render_template, redirect, url_for, request, session, flash, app, Blueprint, jsonify

@app.route('/autocomplete',methods=['GET'])
def autocomplete():
    database='backbone_test'
    db=client[database]
    all_names=list(db.ids.find({},{"current_name":1,"_id":0}))
    return json.dumps(all_names) 

The __init__.py file looks as follows:

from flask import Flask, render_template, Blueprint, jsonify, session
import jinja2

class MyApp(Flask):
  def __init__(self):
    Flask.__init__(self, __name__)
    self.jinja_loader = jinja2.ChoiceLoader([self.jinja_loader,jinja2.PrefixLoader({}, delimiter = ".")])
    def create_global_jinja_loader(self):
        return self.jinja_loader

    def register_blueprint(self, bp):
        Flask.register_blueprint(self, bp)
        self.jinja_loader.loaders[1].mapping[bp.name] = bp.jinja_loader

app = MyApp()

from xyzapp.autocomplete import autocomplete
from xyzapp.blueprint_folder_1.some_file import bp_1

app.register_blueprint(bp_1)

Upvotes: 1

Views: 14877

Answers (4)

Alec1017
Alec1017

Reputation: 119

In autocomplete.py, it looks like you're importing app from Flask and not from your __init__.py file. Try importing from __init__.py instead.

Upvotes: 2

Swift
Swift

Reputation: 1711

I'm sorry this is a bit of a long shot, I'm not familiar with this way of running a Flask app, but it seems you create your own instance of the Flask class called MyApp and initialise it under the variable app.

I'm not 100% and may be completely wrong, but I think your problem lies in the __init__ of MyApp

Upvotes: 0

Alex
Alex

Reputation: 237

import Flask first, the initiate your app

from flask import Flask
app = Flask(__name__)

Upvotes: 2

plaintoast
plaintoast

Reputation: 59

Did you import Flask to your.py? Double check and make the addition of missing.

Upvotes: -1

Related Questions