我对Python Flask非常陌生。我正在开发一个应用程序,在其中创建了以下“ Main.py”脚本。 还有另一个脚本“ run.py”来调用“ create_app”函数并创建该应用程序的实例。
但是它抛出“ RuntimeError:找不到应用程序。在视图函数内部工作或推送应用程序上下文。”错误。
我在我的“ create_app()”函数中尝试了诸如上述here之类的选项。
创建应用后,我尝试将应用也推送到上下文中的“ run.py”文件中。
在使用“ create_app()”函数创建应用程序之后,我尝试在“ run.py”脚本中创建数据库,但是我遇到了以下错误部分所述的错误。
# 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 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/.
错误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'
答案 0 :(得分:1)
with app.app_context():
db.init_app(app)
db.create_all()