# Copyright (C) 2020 The Psycopg Team
+import os
import ctypes
import ctypes.util
import sys
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")