-1

i am running a simple code

from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLALchemy  
from datetime import datetime

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLALchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Integer, default = 0)
    data_created = db.Column(db.datetime, default=datetime.utcnow)

    def __repr__(self):
        return '<Task %r>' % self.id



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

if __name__ == "__main__":
    app.run(debug=True)

and typing this in the terminal

python app.py

but getting error and i dont know the reason for this.

ImportError: cannot import name 'SQLALchemy' from 'flask_sqlalchemy'

Sanidhya Tyagi
  • 45
  • 2
  • 11

1 Answers1

0

Try the solutions below.

1st does not apply if you are using python 3.x

  1. Your question does not have the full context, but I assume the file above is called my_app/flask_alchemy.py or similar and the directory does not contain a __init__.py file. Python needs that file to see a directory as a module that it can import from. Add an empty file called my_app/__init__.py and the problem should be solved

  2. It looks like your issues maybe caused by the file called flask_alchemy.py within the same folder. Try deleting/renaming this file since the name of it will conflict with the real sqlalchemy module...

Edit:

I am thinking that it may be to do with how you installed it.

Try to install it with this command:

pip3 install flask-sqlalchemy 

if it doesnt worked then try above commands with --user at the end of both commands

pip3 install flask-sqlalchemy --user
AzyCrw4282
  • 6,391
  • 5
  • 15
  • 29