第5章 运行flask run报错 AttributeError: Can't set attribute


环境:
Windows 10 20H2
Python 3.9.2
flask 1.1.2
flask-sqlalchemy 2.4.4
python-dotenv 0.15.0
我先按照教程自己敲代码,在flask shell里运行db.create_all()就报这个错误,然后使用示例仓库中的代码运行时,也报这个错误。
看上去应该是flask-sqlalchemy库的问题,请大家帮忙看下,谢谢!

SQLAlchemy 版本是多少?麻烦再贴一下你的模型类和数据库连接配置这些相关代码。

下次贴报错信息,请直接复制终端输出的错误回溯信息,不要用图片的形式发代码或报错,这样别人没法复制里面的文字或代码去运行和查询。

感谢老师这么快就回复。
我直接用的你的示例仓库中截至第5次commit后的源码运行的:


在powershell 运行flask run后报错如下:
(env) PS D:\watchlist2> flask run
 * Environment: development
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 339-670-775
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [17/Mar/2021 08:56:22] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
  File "D:\watchlist2\env\Lib\site-packages\sqlalchemy\util\_collections.py", line 1008, in __call__
    return self.registry[key]
KeyError: <greenlet.greenlet object at 0x000001EAEE871CA0 (otid=0x000001EAEE889280) current active started 
main>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\watchlist2\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\watchlist2\env\Lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\watchlist2\env\Lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\watchlist2\app.py", line 76, in index
    user = User.query.first()
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "D:\watchlist2\env\Lib\site-packages\sqlalchemy\orm\scoping.py", line 128, in __call__
    return self.registry()
  File "D:\watchlist2\env\Lib\site-packages\sqlalchemy\util\_collections.py", line 1010, in __call__       
    return self.registry.setdefault(key, self.createfunc())
  File "D:\watchlist2\env\Lib\site-packages\sqlalchemy\orm\session.py", line 4027, in __call__
    return self.class_(**local_kw)
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 138, in __init__
    bind = options.pop('bind', None) or db.engine
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 943, in engine
    return self.get_engine()
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options        
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "D:\watchlist2\env\Lib\site-packages\flask_sqlalchemy\__init__.py", line 914, in apply_driver_hacks 
    sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

使用的flask-sqlalchemy 2.4.4

SQLAlchemy版本是1.4.0

同样遇到这个问题,估计是flask-sqlalchemy等插件版本的问题,用pipfile.lock里面的版本就没问题,望辉哥解答,谢谢。

看起来是依赖的版本变动导致的问题,我有时间会研究下。临时建议先使用项目中固定的依赖版本:

创建好虚拟环境并激活后,执行下面的命令安装(先切换回 master 分支 git checkout master):

$ pip install -r requirements.txt

或者也可以直接用下面两条命令手动固定一下版本(不用变更 Git 分支):

$ pip install flask-sqlalchemy==2.4.0
$ pip install sqlalchemy==1.3.6
2 Likes

这个问题在 Flask-SQLAlchemy 的这个 PR 修复了,目前包含这个修复 2.5.0 版本已发布,可以使用下面的命令更新 Flask-SQLAlchemy 来解决问题:

$ pip install -U flask-sqlalchemy
1 Like