获取:AttributeError:模块“mariadb”没有 Flask SQLAlchemy 的属性“paramstyle”

问题描述 投票:0回答:1

我正在尝试使用 Flask SQLAlchemy 连接到 mariadb 数据库,但出现以下错误:

AttributeError: module 'mariadb' has no attribute 'paramstyle'

这是配置 URI 并尝试连接的函数:

def create_app():
    # APP SETUP
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
     SQLALCHEMY_DATABASE_URI='mariadb+mariadbconnector://user:pass@localhost:post/database',
    )

    # DATABASE SETUP
    db.init_app(app)
    with app.app_context():
        db.create_all()

    return app

到目前为止,我已执行以下操作来尝试解决该问题:

  1. 基于此答案中的信息:使用 sqlalchemy 用于 mariadb 的 Python 数据库连接 我尝试使用
    mysql+pymysql
    而不是
    mariadb+mariadbconnector
    ,但结果是缺少属性的新来源。运行我的脚本会输出此错误:
AttributeError: module 'pymysql' has no attribute 'paramstyle’
  1. 我的第二次尝试,在同一个线程中单独回答之后,我尝试切换到
    mysql+mysqldb
    ,但这再次将错误源更改为MySQLdb:
AttributeError: module 'MySQLdb' has no attribute 'paramstyle’
  1. 我切换回 MariaDB,并按照此文档:https://mariadb.com/docs/server/connect/programming-languages/c/install/#CS_Package_Repository,安装了以下软件包:
sudo apt install libmariadb3 libmariadb-dev

但仍然没有运气,我遇到了与开始时相同的错误:

AttributeError: module 'mariadb' has no attribute 'paramstyle'

关于为什么在每个库中缺少此 paramstyle 属性还有更多想法吗?我认为这是与 Flask SQLAlchemy 相关的某种错误,因为它是共同点,但我还没有找到任何东西。

谢谢!

这是完整的错误消息:

Traceback (most recent call last):
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 608, in spawn_worker
    worker.init_process()
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 135, in init_process
    self.load_wsgi()
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 147, in load_wsgi
    self.wsgi = self.app.wsgi()
                ^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/base.py", line 66, in wsgi
    self.callable = self.load()
                    ^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 57, in load
    return self.load_wsgiapp()
           ^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 47, in load_wsgiapp
    return util.import_app(self.app_uri)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/util.py", line 370, in import_app
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/runcloud/webapps/spotlessmind-app/wsgi.py", line 3, in <module>
    app = create_app()
          ^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/spotlessmind/__init__.py", line 28, in create_app
    db.init_app(app)
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py", line 374, in init_app
    engines[key] = self._make_engine(key, options, app)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py", line 665, in _make_engine
    return sa.engine_from_config(options, prefix="")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 820, in engine_from_config
    return create_engine(url, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in create_engine
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 612, in create_engine
    dialect = dialect_cls(**dialect_args)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/mysql/mariadbconnector.py", line 143, in __init__
    super().__init__(**kwargs)
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/mysql/base.py", line 2518, in __init__
    default.DefaultDialect.__init__(self, **kwargs)
  File "<string>", line 2, in __init__
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 333, in __init__
    self.paramstyle = self.dbapi.paramstyle
                      ^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'mariadb' has no attribute 'paramstyle'
mariadb flask-sqlalchemy mariadb-connector
1个回答
0
投票

这是一个权限问题,在我的venv目录中,一些库是由运行程序的用户(runcloud)安装的,而其他库是由root安装的。例如:

drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 jinja2
drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 jiter
drwxr-xr-x+  4 root     root       4096 Dec  5 20:21 mariadb
drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 markupsafe
drwxr-xr-x+ 24 root     root       4096 Dec  5 19:06 numpy

这允许 python 程序识别文件夹(并导入库),但无法访问其中的任何内容。因此出现缺失属性错误。

为了解决这个问题,我检查了 root 安装的每个库,并从其中一个功能库(flask)复制了权限和所有权。

这是 mariadb 的示例:

chmod -R --reference=flask mariadb
chown -R --reference=flask mariadb

此后,程序可以导入库并访问其中的文件,没有任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.