【问题标题】:why am I getting this error : AttributeError: 'LocalStack' object has no attribute '__ident_func__' in SQLAlchemy为什么会出现此错误:AttributeError: 'LocalStack' object has no attribute '__ident_func__' in SQLAlchemy
【发布时间】:2022-06-14 16:17:26
【问题描述】:

我正在尝试使用烧瓶创建应用程序,并且在此步骤中,我需要连接本地服务器中的数据库,为此我使用了命令 db = SQLAlchemy(app) 并导入了 SQLALchemy来自烧瓶炼金术(我在下面详细提供了我的代码)。当我启动我的烧瓶应用程序服务器时,我收到错误 AttributeError: 'LocalStack' object has no attribute '--ident_func--' 指向 db = SQLAlchemy 的行(应用程序)。任何帮助将不胜感激,因为我真的不知道我错过了什么。

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__'

【问题讨论】:

  • 尝试将flask-sqlalchemy升级到最新版本(2.5.1)

标签: python flask sqlalchemy flask-sqlalchemy


【解决方案1】:

我真的应该提到我从 GitHub 存储库克隆的源代码,所以尽管使用命令 pip install -r requirement.txt 安装了所有要求,但我猜还是存在影响我的代码文件的代码污染。解决此问题的方法是,我删除了所有项目文件并独立启动了我的项目,而没有来自 GitHub 存储库的任何入门模板。这个项目对我来说只是一个练习,但现在剩下的大问题是如何克隆应用程序并成功安装所有这些依赖项而不会出现错误。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题,我使用python -m pip uninstall flask-sqlalchemy 卸载它并使用python -m pip install flask-sqlalchemy 安装它。问题解决了。

    【讨论】:

      【解决方案3】:

      我可以通过编辑我的requirements.txt 以包含psycopg2-binary 而不是psycopg2 本身来解决此问题。

      之后,我重新构建了 docker 环境(docker-compose build)并重新构建了 docker 环境(docker-compose up)。

      如果以后的读者还没有这样做,我还需要在上述过程之前停用 docker 环境(docker-compose down)。

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题。您遇到上述问题的原因是您的计算机上没有安装 psycopg2。如果您在虚拟环境中工作,请将 psycopg2 添加到您的 requirements.txt 文件,然后通过命令 pip install -r requirements.txt

        安装它

        另外,请确认与本地数据库的连接正常。

        【讨论】:

          猜你喜欢
          • 2023-01-16
          • 2020-05-26
          • 2019-02-09
          • 2015-12-14
          • 2023-02-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多