Hi everyone I’m very beginner using Python and flask. I’m trying to import my database using SQLAlchemy.
I have configured the location, created the database, and created the model as you can see in my app.py file :
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///anomalies.db'
db = SQLAlchemy(app)
class Anomalie (db.Model):
id = db.Column(db.Integer, primary_key=True)
type_anomalie = db.Column(db.String(100), nullable=False)
date_anomalie = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
image_anomalie = db.Column(db.String(100), nullable=False)
name_product = db.Column(db.String, nullable=False)
id_product = db.Column(db.Integer, nullable=False)
def __repr__ (self):
return 'anomalies ' + str(self.id)
@app.route('/anomalies')
def anomalies():
return render_template('anomalies.html', anomalies=all_anomalies)
if __name__ == '__main__':
app.run(debug=True) # with this we can debug
The problem is when I try to import the database from app.py with the follow command from app import db:
Windows PowerShell
Copyright (C) 2015 Microsoft Corporation. Todos los derechos reservados.
PS C:\Users\USER\FlaskProjects\flaskdemo> python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import db
This message appears
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'db' from 'app' (C:\Users\USER\FlaskProjects\flaskdemo\app.py)
>>>
I’m using Visual Studio Code as editor. I have been trying to solve this problem for hours but I don’t know if I’m missing something.
Thanks for your help
this is the structure of my proyect