Subject: ImportError: symbol not found in flat namespace '_PQbackendPID'

From: Gil Sousa <[email-masked]>
Date: Tue, 6 Sep 2022 17:06:28 +0100

Hi all,

I am trying to use psycopg2 for the very first time, though I am stumbling
with an error when trying to import the module:

✗ python3.9 radars_import.py
Traceback (most recent call last):
  File "/Users/gilsousa/Documents/git Projects/ReportGen/radars_import.py",
line 2, in <module>
    import psycopg2
  File
"/Users/gilsousa/Library/Python/3.9/lib/python/site-packages/psycopg2/__init__.py",
line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError:
dlopen(/Users/gilsousa/Library/Python/3.9/lib/python/site-packages/psycopg2/_
psycopg.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace
'_PQbackendPID'

I made sure that I have the packages installed, both psycopg2
and psycopg2-binary. I even tried to uninstall both and just try one at a
time, also didn't work.

How can I fix this problem?

Thank you,

Gil Sousa
www.gilsousa.eu

---

From: Daniele Varrazzo <[email-masked]>
Date: Wed, 7 Sep 2022 01:08:18 +0100

Hello,

On Tue, 6 Sept 2022 at 17:06, Gil Sousa <[email-masked]> wrote:
>
> Hi all,
>
> I am trying to use psycopg2 for the very first time, though I am stumbling with an error when trying to import the module:
>
>   File "/Users/gilsousa/Documents/git Projects/ReportGen/radars_import.py", line 2, in <module>
>     import psycopg2
>   File "/Users/gilsousa/Library/Python/3.9/lib/python/site-packages/psycopg2/__init__.py", line 51, in <module>
> ImportError: dlopen(/Users/gilsousa/Library/Python/3.9/lib/python/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_PQbackendPID'
>
> I made sure that I have the packages installed, both psycopg2 and psycopg2-binary. I even tried to uninstall both and just try one at a time, also didn't work.

You should install only one of them. If you are using an apple M1,
psycopg2-binary doesn't actually install a binary package and falls
back to building from source.

> How can I fix this problem?

I don't have a macOS but the problem has surfaced often, according to
google. It seems related to the wrong installation of the libpq, and
it doesn't help that a few days ago Homebrew has broken the libpq
installation <https://github.com/psycopg/psycopg2/issues/1492>.

See https://gist.github.com/peter-gy/0ebe072acb9065944ecb04d95a4c3096
for an example of someone fixing your problem.

Cheers

-- Daniele