From: Daniele Varrazzo Date: Mon, 5 Sep 2022 09:56:06 +0000 (+0100) Subject: fix(macos): find the libpq in non-standard places X-Git-Tag: 3.1.1~1^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12a99725bcfb0733bfa981315ae97a98899f9110;p=thirdparty%2Fpsycopg.git fix(macos): find the libpq in non-standard places --- 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")