skip to Main Content

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 "pathtoprojectapp.py", line 8, in
db = SQLAlchemy(app)
File "pathtovirtualenvLibsite-packagesflask_sqlalchemyextension.py", line 278, in init
self.init_app(app)
File "pathtovirtualenvLibsite-packagesflask_sqlalchemyextension.py", line 374, in init_app
engines[key] = self._make_engine(key, options, app)
File "pathtovirtualenvLibsite-packagesflask_sqlalchemyextension.py", line 665, in make_engine
return sa.engine_from_config(options, prefix="")
File "pathtovirtualenvLibsite-packagessqlalchemyenginecreate.py", line 820, in engine_from_config
return create_engine(url, **options)
File "pathtovirtualenvLibsite-packagessqlalchemyenginecreate.py", line 599, in create_engine
dbapi = dbapi_meth(**dbapi_args)
File "pathtovirtualenvLibsite-packagessqlalchemydialectspostgresqlpsycopg2.py", line 690, in import_dbapi
import psycopg2
File "pathtovirtualenvLibsite-packagespsycopg2_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

2

Answers


  1. 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.

    Login or Signup to reply.
  2. As noted in the comments in the GitHub issue there is an alternative to downgrading to Python 3.12, which is to upgrade psycopg2 to psycopg 3 instead as this has supported Python 3.13 from version 3.2.2

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search