psycopg / psycopg2

PostgreSQL database adapter for the Python programming language

Home Page:https://www.psycopg.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ImportError `from psycopg2._psycopg import ...`

smbrine opened this issue · comments

This is a bug tracker
If you have a question, such has "how do you do X with Python/PostgreSQL/psycopg2" please write to the mailing list or open a question instead.

Please complete the following information:

  • OS: macOS Sonoma 14.2 @ MBAir M2
  • Psycopg version: 2.9.9
  • Python version: 3.12.1
  • PostgreSQL version: 11.22 (in a docker container but PyCharm connected successfully)
  • pip version 23.3.1

Describe the bug
Please let us know:

1: what you did
I have installed SQLAlchemy and tried to create an async engine. It doesn't really matter whether I create a regular or async engine. Ive tried both psycopg2 and psycopg2-binary.

2: what you expected to happen
I have expected a working postgresql engine :)

3: what happened instead
ImportError was raised.

If possible, provide a script reproducing the issue.

Script:

import os

import sqlalchemy
from dotenv import load_dotenv
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import DeclarativeBase

load_dotenv()

SQLALCHEMY_DATABASE_URL = sqlalchemy.URL.create(
    drivername=os.getenv("SQL_DRIVER"), username=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD"),
    host=os.getenv("POSTGRES_HOST"), port=os.getenv("POSTGRES_PORT"), database=os.getenv("POSTGRES_DB"), )


engine = create_async_engine(
    SQLALCHEMY_DATABASE_URL, echo=True
)


class Base(DeclarativeBase):
    pass
# .env

POSTGRES_USER = 'username'
POSTGRES_PASSWORD = 'password'
POSTGRES_HOST = 'localhost'
POSTGRES_PORT = 5432
POSTGRES_DB = 'postgres'
SQL_DRIVER = 'postgresql'

Traceback:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1527, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "./app/sql_app/create_db.py", line 3, in <module>
    from app.sql_app.database import Base, engine
  File "./app/sql_app/database.py", line 15, in <module>
    engine = create_async_engine(
             ^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.12/site-packages/sqlalchemy/ext/asyncio/engine.py", line 117, in create_async_engine
    sync_engine = _create_engine(url, **kw)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in create_engine
  File "./venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
    return fn(*args, **kwargs)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 601, in create_engine
    dbapi = dbapi_meth(**dbapi_args)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "./venv/lib/python3.12/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 690, in import_dbapi
    import psycopg2
  File "./venv/lib/python3.12/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(./venv/lib/python3.12/site-packages/psycopg2/_psycopg.cpython-312-darwin.so, 0x0002): Library not loaded: @rpath/libpq.5.dylib
  Referenced from: <5F71694B-7580-341A-A0CF-E1068C40A546> ./venv/lib/python3.12/site-packages/psycopg2/_psycopg.cpython-312-darwin.so
  Reason: no LC_RPATH's found

pip freeze:

annotated-types==0.6.0
anyio==3.7.1
asyncpg==0.29.0
cffi==1.16.0
click==8.1.7
cryptography==41.0.7
dnspython==2.4.2
ecdsa==0.18.0
email-validator==2.1.0.post1
fastapi==0.105.0
greenlet==3.0.2
h11==0.14.0
idna==3.6
jwt==1.3.1
passlib==1.7.4
psycopg2==2.9.9
psycopg2-binary==2.9.9
pyasn1==0.5.1
pycparser==2.21
pydantic==2.5.2
pydantic_core==2.14.5
python-dotenv==1.0.0
python-jose==3.3.0
rsa==4.9
six==1.16.0
sniffio==1.3.0
SQLAlchemy==2.0.23
starlette==0.27.0
typing_extensions==4.9.0
uvicorn==0.24.0.post1

We can't fix your laptop and we don't own a macOS M2.