【问题标题】:sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgressqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:postgres
【发布时间】:2020-10-22 13:48:33
【问题描述】:

我正在尝试使用 SQLAlchemy 连接到 Postgres 数据库。我已经安装了 psycopg2。但是,我收到错误sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres。如何配置 SQLAlchemy 以连接到 PostgreSQL?

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgres://username@localhost:5432/template1"

db = SQLAlchemy(app)

【问题讨论】:

    标签: python postgresql sqlalchemy flask-sqlalchemy psycopg2


    【解决方案1】:

    URI 应以postgresql:// 开头,而不是postgres://。 SQLAlchemy 过去接受这两者,但已删除对 postgres 名称的支持。

    【讨论】:

      【解决方案2】:

      SQLAlchemy 1.4 删除了已弃用的postgres 方言名称,现在必须使用名称postgresql。方言是 URL 中:// 之前的部分。 SQLAlchemy 1.3 及更早版本显示弃用警告,但仍接受它。

      要解决此问题,请将 URL 中的 postgres:// 重命名为 postgresql://

      当前使用 Heroku 时会出现此错误,它在他们提供的 DATABASE_URL 中使用 postgres,您可能将其用于 SQLALCHEMY_DATABASE_URI。要在他们更新之前解决此问题,请将 Heroku 仪表板中的变量更新为使用 postgresql

      【讨论】:

      • 如果变量是指配置变量,我尝试这样做并继续获得Item could not be updated: Cannot overwrite attachment values DATABASE_URL
      • @Daniyaldehleh 我无法真正说出真正的问题是什么,因为你没有给我足够的信息来说明你到底做了什么。请让我知道您到底尝试了什么,我可以提供帮助
      • @Daniyaldehleh 我在您的 Python 脚本中使用了一个简单的替换方法,而不是在 Heroku 仪表板中,因为正如您提到的那样,这不起作用。所以你可以这样做:SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL?sslmode=require').replace('postgres://', 'postgresql://').
      • 谢谢大家!我实际上最终联系了 heroku,他们给了我这个解决问题的文档 (help.heroku.com/ZKNTJQSK/…)。代码:uri = os.getenv("DATABASE_URL") # or other relevant config var if uri.startswith("postgres://"): uri = uri.replace("postgres://", "postgresql://", 1)
      • 添加上面的代码并尝试运行app.py后,输出如下错误:AttributeError: 'NoneType' object has no attribute 'replace'
      【解决方案3】:

      使用简单的python url替换代码解决了heroku中的问题

      import os
      import re
      
      uri = os.getenv("DATABASE_URL")  # or other relevant config var
      if uri and uri.startswith("postgres://"):
          uri = uri.replace("postgres://", "postgresql://", 1)
      # rest of connection code using the connection string `uri`
      

      来源:https://help.heroku.com/ZKNTJQSK/why-is-sqlalchemy-1-4-x-not-connecting-to-heroku-postgres

      【讨论】:

        【解决方案4】:

        URI 应该以 postgresql:// 而不是 postgres:// 开头 SQLAlchemy 过去接受这两者,但已删除对 Postgres 名称的支持。

        【讨论】:

          【解决方案5】:

          完整的 URI 应该是:

          app.config["SQLALCHEMY_DATABASE_URI"] ="postgresql://postgres:password@localhost:5432/template1"
          

          app.config["SQLALCHEMY_DATABASE_URI"]= f'postgresql://{db_user}:{db_pswd}@{db_host}/{db_name}'

          【讨论】:

            【解决方案6】:

            我也遇到了上述问题,并通过将 postgres:// 替换为 postgresql:// 来解决 而且工作正常。

            【讨论】:

              猜你喜欢
              • 2021-06-15
              • 2023-02-05
              • 2020-03-27
              • 2017-06-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多