我需要将以下代码升级为基于psycopg
版本3的等效代码:
import psycopg2
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://', creator=connector)
这个psycopg2的网址工作起来像一个魅力,但是:
import psycopg # v3.1
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg://', creator=connector)
(我也试过"psycopg3"这个词,没有成功)
退货:
Traceback (most recent call last):
File "/tmp/ipykernel_1032556/253047102.py", line 1, in <cell line: 1>
engine = create_engine('postgresql+psycopg://', creator=connector)
File "<string>", line 2, in create_engine
File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/create.py", line 534, in create_engine
entrypoint = u._get_entrypoint()
File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/url.py", line 661, in _get_entrypoint
cls = registry.load(name)
File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/util/langhelpers.py", line 343, in load
raise exc.NoSuchModuleError(
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgresql.psycopg
那么,如何正确地创建一个基于psycopg
(v3.x)的SQLAlchemy引擎呢?
我的sqlalchemy
版本是:"1.4.35"(尝试版本1.4.40
,但遇到AttributeError: module 'sqlalchemy' has no attribute 'dialects'
错误)。
psycopg3文件:https://www.psycopg.org/psycopg3/docs/api/
SQL炼金术文档:https://docs.sqlalchemy.org/en/14/core/engines.html
1条答案
按热度按时间h79rfbju1#
SQLAlchemy在1.4.x系列中不支持psycopg 3。+符号后面的部分是sqlalchemy. dialects. postgresql.{engine}中的一个包,其中engine是psycopg 2、asyncpg或其他内容。
其他方言也是如此。
但是在1.4.x系列中没有psycopg和psycopg 3驱动程序。