创建引擎对象以连接postgresql数据库时发生Python sqlalchemy错误



我在python中使用sqlalchemy连接到PostgreSQL数据库中的数据库,但出现错误。有人能帮我吗?

以下是运行该文件后的命令行输出。

C:UsersnaeemDesktop>python sql.py
Traceback (most recent call last):
File "sql.py", line 5, in <module>
engine = create_engine("postgressql://postgres:postgres@localhost:5432/flights")
File "<string>", line 2, in create_engine
File "C:Program FilesPython37libsite-packagessqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.eggsqlalchemyutildeprecations.py", line 171, in warned
return fn(*args, **kwargs)
File "C:Program FilesPython37libsite-packagessqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.eggsqlalchemyenginecreate.py", line 437, in create_engine
entrypoint = u._get_entrypoint()
File "C:Program FilesPython37libsite-packagessqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.eggsqlalchemyengineurl.py", line 172, in _get_entrypoint
cls = registry.load(name)
File "C:Program FilesPython37libsite-packagessqlalchemy-1.4.0b1.dev0-py3.7-win-amd64.eggsqlalchemyutillanghelpers.py", line 278, in load
"Can't load plugin: %s:%s" % (self.group, name)
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgressql

这是我的sql.py 的python代码

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine("postgressql://postgres:postgres@localhost:5432/flights")
db = scoped_session(sessionmaker(bind=engine))
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

我认为您在"create_engine"函数中拼错了postgres你放了2个s字符而不是1个

示例:

from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine("postgresql://postgres:postgres@localhost:5432/flights")
db = scoped_session(sessionmaker(bind=engine))
flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
for flight in flights:
print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")```

我认为您应该将方言指定为postgres://而非postgressql://像这样:

engine = create_engine("postgres:// ...

这就是sqlalchemy通知您没有名为postgressql的模块的原因。

相关内容

  • 没有找到相关文章

最新更新