我对Python Flask还很陌生。我正在开发一个应用程序,在那里我创建了以下";Main.py";剧本还有另一个脚本";run.py";呼叫";create_app"函数并创建应用程序的实例。
但它正在抛出">运行时错误:找不到应用程序。要么在视图函数内部工作,要么推送应用程序上下文";错误
我在我的";create_app(("作用
我已经尝试将应用程序推送到我的";run.py";文件,在创建应用程序之后。
我试着在";run.py";在使用";create_app(("函数,但我得到了如下错误部分所述的错误。
# 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
我有另一个剧本";run.py",它创建一个应用程序并运行它。
# 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)
错误-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/.
错误-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'
with app.app_context():
db.init_app(app)
db.create_all()