TransWikia.com

Python Flask Context - Unable to Create DB

Stack Overflow Asked by Sujeet Padhi on July 29, 2020

I am quite new to Python-Flask. I am developing an app, where I have created the below "Main.py" script.
There is another script "run.py" to call "create_app" function and create an instance of the app.

But it is throwing "RuntimeError: No application found. Either work inside a view function or push an application context." error.

I had tried options such as mentioned here, inside my "create_app()" function.

I have tried pushing the app to the context as well inside my "run.py" file, after the app is created.

I tried creating the database inside "run.py" script after creating the application using "create_app()" function, but I am getting the errors as mentioned below in error section.

# Main.py Script Inside my application package:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app():
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
    db.init_app(app)
    db.create_all()
    return app

I have another script "run.py" , which creates an app and runs it.

# run.py to create an instance of app and run it

from flaskblog import create_app

app = create_app()
# app.app_context().push()  # Tried this

# db.init_app(app)    # Tried this as well, after commenting the same line in "create_app()" function
# db.create_all()

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

Error-1

PS Python_Flask> & "C:/Program Files/Python38/python.exe" c:/Document/Codes/Python_Flask/run.py
C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
  File "c:/Document/Codes/Python_Flask/run.py", line 3, in <module>
    app = create_app()
  File "c:DocumentCodesPython_Flaskflaskblog__init__.py", line 52, in create_app
    db.create_all()
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 1016, in _execute_for_all_tables
    app = self.get_app(app)
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 987, in get_app
    raise RuntimeError(
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

Error-2

PS Python_Flask> & "C:/Program Files/Python38/python.exe" c:/Document/Codes/Python_Flask/run.py
C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  warnings.warn(FSADeprecationWarning(
Traceback (most recent call last):
  File "c:/Document/Codes/Python_Flask/run.py", line 3, in <module>
    app = create_app()
  File "c:DocumentCodesPython_Flaskflaskblog__init__.py", line 54, in create_app
    db.create_all()
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:Program FilesPython38libsite-packagesflask_sqlalchemy__init__.py", line 883, in apply_driver_hacks
    if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'

One Answer

with app.app_context():
    db.init_app(app)
    db.create_all()

Correct answer by vestronge on July 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP