pythonpostgresqlflaskflask-sqlalchemypsycopg2

I get this error - ImportError: DLL load failed while importing _psycopg: The specified module could not be found. what can I do about it?


I'm working on a Flask project with SQLAlchemy and PostgreSQL. When I try to run the Flask app, I encounter the following error:

Error: While importing 'app', an ImportError was raised:

Traceback (most recent call last): File "path\to\project\app.py", line 8, in db = SQLAlchemy(app) File "path\to\virtualenv\Lib\site-packages\flask_sqlalchemy\extension.py", line 278, in init self.init_app(app) File "path\to\virtualenv\Lib\site-packages\flask_sqlalchemy\extension.py", line 374, in init_app engines[key] = self._make_engine(key, options, app) File "path\to\virtualenv\Lib\site-packages\flask_sqlalchemy\extension.py", line 665, in make_engine return sa.engine_from_config(options, prefix="") File "path\to\virtualenv\Lib\site-packages\sqlalchemy\engine\create.py", line 820, in engine_from_config return create_engine(url, **options) File "path\to\virtualenv\Lib\site-packages\sqlalchemy\engine\create.py", line 599, in create_engine dbapi = dbapi_meth(**dbapi_args) File "path\to\virtualenv\Lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 690, in import_dbapi import psycopg2 File "path\to\virtualenv\Lib\site-packages\psycopg2_init.py", line 51, in from psycopg2._psycopg import ( # noqa ... ImportError: DLL load failed while importing _psycopg: The specified module could not be found.

Environment Details: Python version: Python 3.13.0 Flask version: Flask 3.1.0 SQLAlchemy version: SQLAlchemy 2.0.36 psycopg2 version: psycopg2-binary : Version: 2.9.10 PostgreSQL version: psql (PostgreSQL) 17.2 OS: Windows 10 22H2

Steps I've Taken:

  1. Installed psycopg2-binary instead of psycopg2 (tried both none of them worked)
  2. Reinstalled Visual C++ Redistributable to the latest version
  3. Added Path file in system environment for Postgresql/bin

Solution

  • Python 3.13 is not currently supported by psycopg2 and psycopg2-binary on Windows machine. Use Python 3.12 instead. See the available wheels for specific platforms on psycopg2-binary and psycopg2.