From 12a99725bcfb0733bfa981315ae97a98899f9110 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 5 Sep 2022 10:56:06 +0100 Subject: [PATCH] fix(macos): find the libpq in non-standard places --- psycopg/psycopg/pq/_pq_ctypes.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/psycopg/psycopg/pq/_pq_ctypes.py b/psycopg/psycopg/pq/_pq_ctypes.py index 43dfc1ad4..58b9bd4b9 100644 --- a/psycopg/psycopg/pq/_pq_ctypes.py +++ b/psycopg/psycopg/pq/_pq_ctypes.py @@ -4,6 +4,7 @@ libpq access using ctypes # Copyright (C) 2020 The Psycopg Team +import os import ctypes import ctypes.util import sys @@ -13,12 +14,33 @@ from typing import List, Optional, Tuple from ..errors import NotSupportedError +import logging + +logger = logging.getLogger("psycopg") + + if sys.platform == "win32": libname = ctypes.util.find_library("libpq.dll") + elif sys.platform == "darwin": libname = ctypes.util.find_library("libpq.dylib") + # (hopefully) temporary hack: libpq not in a standard place + # https://github.com/orgs/Homebrew/discussions/3595 + # If pg_config is available and agrees, let's use its indications. + if not libname: + try: + import subprocess as sp + + libdir = sp.check_output(["pg_config", "--libdir"]).strip().decode() + libname = os.path.join(libdir, "libpq.dylib") + if not os.path.exists(libname): + libname = None + except Exception as ex: + logger.debug("couldn't use pg_config to find libpq: %s", ex) + else: libname = ctypes.util.find_library("pq") + if not libname: raise ImportError("libpq library not found") -- 2.47.2