我正在尝试运行
alembic
迁移,当我运行 时
alembic revision --autogenerate -m "Added initial tables"
它失败了
sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver
数据库网址是
postgresql+psycopg2://dev:passwd@localhost/db
我什至在我的 virtualenv 中安装了
psycopg2
$yolk -l
Flask-Login - 0.1.3 - active
Flask-SQLAlchemy - 0.16 - active
Flask - 0.9 - active
Jinja2 - 2.6 - active
Mako - 0.7.3 - active
MarkupSafe - 0.15 - active
Python - 2.7.2 - active development (/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload)
SQLAlchemy - 0.8.0 - active
Werkzeug - 0.8.3 - active
alembic - 0.4.2 - active
antiorm - 1.1.1 - active
appscript - 1.0.1 - active
distribute - 0.6.27 - active
envoy - 0.0.2 - active
osascript - 0.0.4 - active
pep8 - 1.4.5 - active
pip - 1.1 - active
psycopg2 - 2.4.6 - active
wsgiref - 0.1.2 - active development (/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7)
yolk - 0.4.3 - active
什么原因可能导致此问题?
>>> from sqlalchemy import *
>>> create_engine("driver://")
Traceback (most recent call last):
... etc
sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver
所以我想说你实际上并没有使用你认为的 postgresql URL - 你可能正在某处调用默认生成的 alembic.ini 。
zzzzeek指的“默认生成的alembic.ini”位于项目的根目录中。
整个问题是在sqlalchemy.url
文件中设置
alembic.ini
配置参数之一。此外,还可以按照https://stackoverflow.com/a/15668175/973380中的说明以编程方式进行设置。
方言:该方案的形式为 dialect://
或
dialect+driver://
。例如,连接到 PostgreSQL 数据库的正确 URL 将以
postgresql://
(默认使用
psycopg2
)开头,或显式选择驱动程序(
postgresql+psycopg2://
,或使用其他驱动程序)。如果您碰巧指定了
only psycopg2
,您将收到错误
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:psycopg2
alembic.ini
中的默认值,您可以在
alembic/env.py
中添加一两行代码:
+ import app
[...snip...]
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
+ config.set_section_option(
config.config_ini_section, "sqlalchemy.url", app.settings.SQLALCHEMY_DATABASE_URL
)
其中 import app
和
app.settings.SQLALCHEMY_DATABASE_URL
替换为您自己的应用程序特定代码以检索 URL。
即将此行重命名为
sqlalchemy.url = sqlite:///name_of_my_database.db
create_engine(f"<dialect_name>+<driver_name>://{self.user}:{self.password}@{self.host}:{self.port}/{self.database})
并且此处的其他建议都无法解决您的问题,您可以尝试下一段中概述的解决方案。问题
NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:\<dialect_name\>.\<driver_name\>
可能是由于运行支持您所需驱动程序的 SQLAlchemy 版本引起的。例如,SQLAlchemy 1.4.26 支持这些 PostgreSQLdrivers。所以,解决方案:
pip uninstall SQLAlchemy
pip install SQLAlchemy==1.0.14
connection_url=""
#your database connection string here pulled from either environment variable , vaults or can be set directly
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = connection_url
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
configdict=config.get_section(config.config_ini_section)
configdict.update({"sqlalchemy.url":connection_url})
connectable = engine_from_config(
configdict,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
如果你想从 python 文件本身读取连接 url 而不引用默认值
alembic.ini配置文件。
sudo apt-get install libpq-dev
sudo pip install psycopg2
sudo pip install redshift-sqlalchemy
sudo pip install sqlparse
来自:
import sqlalchemy as sa
user, pasw, hostname = UserName,Password, 'myurl.com'
# connect
td_engine = sa.create_engine('teradata://{}:{}@{}:22/'.format(user,pasw,hostname),echo=True)
df = pd.read_sql_query(query1,connect)
致:
import teradata
user, pasw, hostname = UserName,Password, 'myurl.com'
td = teradata.UdaExec (appName="test", version="1.0", logConsole=True)
td_engine = td.connect(method="odbc",system=hostname, username=user,password=pasw,driver="Teradata")
pip install ibm_db_sa
解决了问题