为什么我会遇到此错误:attributeError:' localstack'对象没有属性' __IDEND_FUNC __'在Sqlalchemy
我正在尝试使用烧瓶创建一个应用程序,并且我需要在本地服务器中连接我的数据库,以实现此目的,我已经使用了命令db = sqlalchemy(app)并从烧瓶中导入sqlalchemy炼金术(我提供了详细的代码)。当我启动烧瓶应用程序服务器时,我会得到错误attributeError:'localstack'对象没有属性' - 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冻结(需求.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__'
I am trying to create an app with flask, and I am in this step that I need to connect my database which is in my local server, to achieve this I have used the command db = SQLAlchemy(app) and imported SQLALchemy from flask-alchemy(I have provided my codes bellow in detail).when I start my flask app server I get the error AttributeError: 'LocalStack' object has no attribute '--ident_func--' pointing to the line with db = SQLAlchemy(app). Any help will be appreciated cause I really don't know what I am missing.
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 freeze (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
error massage on terminal
(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__'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我遇到了同样的问题。我尝试了一些方法,但它们没有起作用。当我尝试以下命令升级烧瓶塞拉尔奇米和sqlalchemy时。
I had the same problem. I tried some ways but they didn't work. When I tried below commands upgrade both Flask-SQLAlchemy and SQLAlchemy it worked.
我真的应该提到,我从github存储库中克隆的源代码,因此,尽管使用命令
pip install -r sightmement.txt
> 。解决此问题的工作是,我删除了所有项目文件,并独立启动了我的项目,而无需从GitHub存储库中进行任何启动模板。这个项目对我来说只是一个练习练习,但是现在仍然存在的大问题是如何克隆应用程序并成功安装所有这些依赖性而无需遇到错误。I really should have mentioned that the source codes I had cloned from a GitHub repo so there was code pollution that affected my code files I guess despite installing all the requirements with the command
pip install -r requirement.txt
. A work around this is that I deleted all my project files and started my project independently without any starter templates from the GitHub repo. This project was just a practice exercise for me but the big question which now remains is how to clone app and successfully install all these dependency without getting the error.我也有类似的问题。您之所以遇到上述问题的原因是因为您在机器上没有安装 psycopg2 。如果您是从虚拟环境中工作的,则将 psycopg2 添加到您的 supports.txt文件,然后通过命令
pip install -restind -r unigess.txt
安装它。另外,请确认与本地数据库的连接正在工作。
I had a similar problem. The reason you are experiencing the above issue is because you don't have psycopg2 installed on you machine. If you are working from a virtual environment add psycopg2 to your requirements.txt file then install it via the command
pip install -r requirements.txt
Also, confirm that the connection to your local database is working.
我能够通过编辑我的
sumplast.txt
来解决此问题,以包括psycopg2-binary
,而不是psycopg2
本身。之后,我重新构建了Docker环境(
Docker-Compose Build
),然后重新启动Docker环境(Docker-Compockose
)。如果未来的读者尚未这样做,我还需要在上述过程之前停用Docker环境(
Docker-Compose down
)。I was able to fix this problem by editing my
requirements.txt
to include thepsycopg2-binary
instead ofpsycopg2
itself.Afterwards, I re-built the docker environment (
docker-compose build
) and re-up'd the docker environment (docker-compose up
).In case future readers have not done so yet, I also needed to deactivate the docker environment before the above process (
docker-compose down
).我遇到了相同的问题
“ attributeError:'_fakestack'对象没有属性'__ident_func __'''
,我通过升级Blask-sqlalchemy解决了它:I had the same problem
"AttributeError: '_FakeStack' object has no attribute '__ident_func__'"
and I solved it by upgrading Flask-SQLAlchemy:这个问题的根本原因是,烧瓶依赖的Werkzeug软件包的更改需要更改烧瓶构造范围的会话的方式。该更改记录在
升级到烧瓶塞拉尔奇米&gt; = 2.5.1应该解决问题。
The root cause of this issue is that a change to the Werkzeug package - which Flask depends on - required a change to how Flask constructs scoped sessions. The change is documented here. The error occurs if the Werkzeug package has its change but the Flask package does not.
Upgrading to Flask-SQLAlchemy >= 2.5.1 should solve the issue.
我也有一个类似的问题,我使用
python -m Pip卸载烧瓶 - 塞拉尔奇米
卸载了它,并使用python -m pip install flask install flask -sqlalchemy
安装。问题解决了。I had a similar issue, I uninstalled it using
python -m pip uninstall flask-sqlalchemy
and installed withpython -m pip install flask-sqlalchemy
. The issue was solved.