0

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

proyect structure

  • I executed this code on my machine and it ran fine. – Debdut Goswami Jul 02 '20 at 13:19
  • It might be strange but I am able to run it successfully. – Astik Gabani Jul 02 '20 at 13:19
  • Have you tried to initiate the db before configuring SQLALCHEMY_DATABASE_URI??? And if doesn't work try to import db by opening python shell one level directory before i.e from C:\Users\USER\FlaskProjects\ – Aditya Jul 02 '20 at 13:19
  • @Aditya , I did all that you told me but still not working :/ headache – Lady Geraldine Villamil Guerre Jul 02 '20 at 14:50
  • you need first to **activate** the virtual environment on PS (have a look at my answer on this https://stackoverflow.com/a/62534280/12368419) and then `Flask shell` command, (also have a look at my answer on this https://stackoverflow.com/a/61557598/12368419) – cizario Jul 02 '20 at 15:41
  • I have to add that when I try to import it, a new folder is created called `__pychache__` whith one file in it called `app.cpython-38.pyc` I think that is creating the conflict – Lady Geraldine Villamil Guerre Jul 02 '20 at 16:22
  • `__pycache__` is fine; it's just where Python stores some files to help it run faster. I noticed in your screenshot the file isn't saved; did you add the `db` without saving? – Brett Cannon Jul 03 '20 at 00:42

0 Answers0