我正在尝试用烧瓶创建一个应用程序,在这个步骤中,我需要连接本地服务器中的数据库,为此,我使用命令db = SQLAlchemy( app )并从烧瓶-炼金术中导入SQLALchemy (我提供了详细的代码),启动了烧瓶应用服务器,我得到了错误AttributeError:'LocalStack‘object没有属性'--ident_func--’指向db = SQLAlchemy( app )的行。任何帮助都会受到感谢,因为我真的不知道我错过了什么。
app.py
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
#----------------------------------------------------------------------------#
# Configs
#----------------------------------------------------------------------------#
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
#----------------------------------------------------------------------------#
# Models
#----------------------------------------------------------------------------#
#to include my database schema models later
#----------------------------------------------------------------------------#
# routes
#----------------------------------------------------------------------------#
@app.route('/')
def index():
return render_template('pages/home.html')
if __name__=='__main__':
app.run()
config.py
# Enable debug mode.
DEBUG = True
# Connect to the database
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres@localhost:5432/fyyurdb'
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
pip冻结(requirements.txt)
Babel==2.9.0
click==8.1.3
colorama==0.4.4
Flask==2.1.2
Flask-Moment==0.11.0
Flask-SQLAlchemy==2.4.4
Flask-WTF==0.14.3
greenlet==1.1.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
python-dateutil==2.6.0
pytz==2022.1
six==1.16.0
SQLAlchemy==1.4.36
Werkzeug==2.1.2
WTForms==3.0.1
终端错误按摩
(fyyurenv) C:\Users\Gm Kahosh\Desktop\fyyur\cd0046-SQL-and-Data-Modeling-for-the-Web>python app.py
Traceback (most recent call last):
File "C:\Users\Gm Kahosh\Desktop\fyyur\cd0046-SQL-and-Data-Modeling-for-the-Web\app.py", line 13, in <module>
db = SQLAlchemy(app)
File "C:\Users\Gm Kahosh\Desktop\fyyur\fyyurenv\lib\site-packages\flask_sqlalchemy\__init__.py", line 714, in __init__
self.session = self.create_scoped_session(session_options)
File "C:\Users\Gm Kahosh\Desktop\fyyur\fyyurenv\lib\site-packages\flask_sqlalchemy\__init__.py", line 747, in create_scoped_session on
scopefunc = options.pop('scopefunc', _app_ctx_stack.__ident_func__)
AttributeError: 'LocalStack' object has no attribute '__ident_func__'
发布于 2022-06-03 17:18:48
我也遇到了类似的问题,我使用python -m pip uninstall flask-sqlalchemy
卸载了它,并安装了python -m pip install flask-sqlalchemy
。这个问题已经解决了。
发布于 2022-06-14 08:17:26
我能够通过编辑我的requirements.txt
来包含psycopg2-binary
而不是psycopg2
本身来解决这个问题。
之后,我重新构建了码头环境(docker-compose build
)和码头环境(docker-compose up
).
如果将来的读者还没有这样做,我还需要在上面的过程(docker-compose down
)之前禁用docker环境。
发布于 2022-08-24 16:48:37
造成此问题的根本原因是,对Werkzeug包的更改--这是Flask所依赖的--需要更改Flask构造作用域会话的方式。变更记录在这一变化中。如果Werkzeug包有它的更改,但是Flask包没有更改,则会发生错误。
升级到烧瓶-SQLAlchemy >= 2.5.1应该可以解决这个问题。
https://stackoverflow.com/questions/72256625
复制相似问题