0

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 Files\Python38\lib\site-packages\flask_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:\Document\Codes\Python_Flask\flaskblog\__init__.py", line 52, in create_app
    db.create_all()
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 1016, in _execute_for_all_tables
    app = self.get_app(app)
  File "C:\Program Files\Python38\lib\site-packages\flask_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 Files\Python38\lib\site-packages\flask_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:\Document\Codes\Python_Flask\flaskblog\__init__.py", line 54, in create_app
    db.create_all()
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:\Program Files\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 883, in apply_driver_hacks
    if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
Sujeet Padhi
  • 254
  • 1
  • 20
  • Have you visited this as mentioned in error 1 http://flask-sqlalchemy.pocoo.org/contexts/ I also faced the same error and this worked for me – Aditya Jul 22 '20 at 07:53
  • @Aditya : I have visted that page and tried the options, but still not working. I have commented the lines, what all I have tried already. – Sujeet Padhi Jul 22 '20 at 07:56
  • @Aditya : I have simplified the query. Can you check now, if you can help. – Sujeet Padhi Jul 22 '20 at 08:21
  • is main.py under package flaskblog? if yes, use from flaskblog.main import create_app – karsas Jul 22 '20 at 16:00

1 Answers1

1
with app.app_context():
    db.init_app(app)
    db.create_all()
vestronge
  • 897
  • 6
  • 10