How to fix SQLAlchemy Can't load plugin: sqlalchemy.dialects:cockroachdb

Problem

When using SQLAlchemy to connect to CockroachDB, using a database URL such as cockroachdb+psycopg2://root@localhost:26257/defaultdb you encounter the following error:

 File "/home/uli/.local/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "/home/uli/.local/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 550, in create_engine
    entrypoint = u._get_entrypoint()
                 ^^^^^^^^^^^^^^^^^^^
  File "/home/uli/.local/lib/python3.12/site-packages/sqlalchemy/engine/url.py", line 758, in _get_entrypoint
    cls = registry.load(name)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/uli/.local/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 375, in load
    raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:cockroachdb

Solution

You have not installed the sqlalchemy-cockroachdb package.

To fix this, run:

pip install sqlalchemy-cockroachdb

and also add it to your requirements.txt file if you have one.