Flask SQLAlchemy无法在Venv中设置属性错误 [英] Flask SQLAlchemy can't set attribute error in a venv

查看:44
本文介绍了Flask SQLAlchemy无法在Venv中设置属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask-SQLAlchemy 学习身份验证功能,因此构建了非常基本的应用程序,用户(属性:名称,电子邮件和密码)可以进行注册和登录,并获得重定向到虚拟的个人资料页面,仅此而已.令人费解的行为是,如果我使用虚拟环境,那么我完成的教程将无法完全解决问题.

I'm learning authentication functionality with Flask-SQLAlchemy, so building out very basic apps where a user (attributes: name, email and password) is able to sign-up and login, get redirected to a dummy profile page, and that's it. The puzzling behaviour is the tutorials I've done won't work all the way through if I use a virtual environment.

要显示此行为,请遵循

To show this behaviour, follow this digital ocean tutorial. At step 6, we have:

>>> from project import db, create_app
>>> db.create_all(app=create_app())

如果我有 auth 环境处于活动状态(如本教程所述,我应该如此),则会收到以下错误消息:

If I have the auth environment active (as the tutorial states I should), I get the following error message:

/.../flask_auth_app/venv/lib/python3.8/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 "<stdin>", line 1, in <module>
  File "/.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "/.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "/.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "/.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "/.../flask_auth_app/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "/.../flask_auth_app/venv/lib/python3.8/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

如果我在 localhost 上运行应用程序时尝试注册一个新用户,将会遇到相同的错误-也就是说,如果不这样做,我只能成功注册一个新用户运行一个虚拟环境,以便该应用改为从已安装软件包的全局版本读取.

I'll get the same error if I try to sign up a new user while running the app on localhost - that is, I can only successfully sign up a new user if I don't have a virtual environment running, so that the app reads from global versions of installed packages instead.

如果我在运行 python3 之前取消激活环境,此错误就会消失.如果遵循本教程,我将得到相同的行为.,在此处给出的示例代码.

This error goes away if I deactivate the environment before running python3. I'll get the same behaviour if I follow this tutorial, example code given here.

这在 venv 〜/miniconda3/lib/python3.8/site-packages 中是相同的.我使用的是MacOS Big Sur v11.2.3.

This is the same in venv and ~/miniconda3/lib/python3.8/site-packages. I'm on a MacOS Big Sur v11.2.3.

Flask==1.1.2
Flask-Login==0.5.0
Flask-SQLAlchemy==2.4.4
SQLAlchemy==1.4.0

推荐答案

我最近也遇到了同样的错误消息.实际上,这是由于SQLAlchemy的升级,该升级是作为flask-sqlalchemy的依赖项安装的.

I recently delt with that same error message. It is actually due to an upgrade in SQLAlchemy, which gets installed as a dependency of flask-sqlalchemy.

您可以找到我在stackoverflow上发布的有关该问题的问题

You can find the question I posted on stackoverflow for the issue here:

通过卸载SQLAlchemy == 1.4.0并安装先前版本的SQLAlchemy == 1.3.23可以解决此问题.

The problem gets solved by uninstalling SQLAlchemy==1.4.0 and installing the previous version SQLAlchemy==1.3.23.

尝试在虚拟环境中执行此操作,看看是否有帮助.

Try doing that in your virtual environ and see if it helps.

这篇关于Flask SQLAlchemy无法在Venv中设置属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆