I created a Flask app with SQLAlchemy.
I initialised the database in app.py
but I want to use it in another file: otherfile.py
.
app.py
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///my_database'
db = SQLAlchemy(app)
print("TEST")
otherfile.py
from app import db
print(db)
But I get the error.
ImportError: cannot import name 'db'
Basically, I am doing this because I do not want to write the SQLAlchemy logic in app.py
but I want to keep it in another file.
In addition, I just want the variable db
to be exported. I do not want that when I run otherfile.py
, this runs also print("TEST")
which is in app.py
I looked at these answer with little luck:
How to share the global app object in flask?