Psycopg2 error: Symbol not found _PQbackendPID
Asked Answered
I

7

43

I'm getting the following error from psycopg2:

Traceback (most recent call last):   File "test1.py", line 1, in
<module>
    import psycopg2   File "/Users/xxx/Library/Python/2.7/lib/python/site-packages/psycopg2/__init__.py",
line 50, in <module>
    from psycopg2._psycopg import (                     # noqa ImportError:
dlopen(/Users/xxx/Library/Python/2.7/lib/python/site-packages/psycopg2/_psycopg.so,
2): Symbol not found: _PQbackendPID   Referenced from:
/Users/xxx/Library/Python/2.7/lib/python/site-packages/psycopg2/_psycopg.so
Expected in: flat namespace  in
/Users/eyabadal/Library/Python/2.7/lib/python/site-packages/psycopg2/_psycopg.so

Any suggestions on how to fix this?

Inverse answered 12/10, 2018 at 0:46 Comment(0)
R
91

I faced the same issue on macOS and solved it by running below steps:

  1. Uninstall psycopg2: pip uninstall psycopg2

  2. Re-install psycopg2: pip install psycopg2-binary

Ritz answered 24/5, 2019 at 4:20 Comment(4)
Worked for me on my new MPB M1.Illeetvilaine
worked like magic, why we were getting error with psycopg2 ?Suck
I had to run a pip3 uninstall psycopg2-binary to get the uninstall going.Louvain
@Suck In my case, the issue was related to trying to run code built for Intel chips, on a M1 Mac with "Apple Silicon", after originally migrating from a machine with Intel chips. I had to run these commands inside "arch -x86_64 zsh", after setting up parallel installations of Homebrew and pip to let me switch between running directly on the local chips and emulating the Intel chips. I suspect the general answer is that you've somehow installed a version of psycopg2 which doesn't match your hardware... Apple claims it detects such cases and prompts the user, but ...Charlacharlady
A
30

I had the same issue, installing psycopg2-binary with option --no-cache-dir helped me.

pip install psycopg2-binary --no-cache-dir

Absinthism answered 25/12, 2021 at 15:22 Comment(2)
This is the only answer that worked for me on MBP M1Siphonostele
Same for me but M1 Pro chipDante
L
4

For anyone still having issues I have used the following:

pip install -i https://test.pypi.org/simple/ psycopg2-binary==2.9.3

You can find comments here: https://github.com/psycopg/psycopg2/issues/1286#issuecomment-1190350326

Lithe answered 28/7, 2022 at 15:25 Comment(0)
P
2

I've gone through a number of these, and I think for those who are unresolved otherwise should take a look at the potential problem. I'm guessing many people are coming from M1 Macs, and may or may not have Postgres.app installed.

If you add -v to the end of your pip3 install psycopg2, then you'll see the source of the error towards the bottom. In my case I got:

  ld: warning: ignoring file /Applications/Postgres.app/Contents/Versions/14/lib/libcrypto.dylib, file is universal (x86_64,arm64) but does not contain the arm64e architecture: /Applications/Postgres.app/Contents/Versions/14/lib/libcrypto.dylib
  ld: warning: ignoring file /Applications/Postgres.app/Contents/Versions/14/lib/libpq.dylib, file is universal (x86_64,arm64) but does not contain the arm64e architecture: /Applications/Postgres.app/Contents/Versions/14/lib/libpq.dylib
  ld: warning: dylib (/Applications/Postgres.app/Contents/Versions/14/lib/libssl.dylib) was built for newer macOS version (10.12) than being linked (10.9)
  ld: warning: ld: warning: dylib (/Applications/Postgres.app/Contents/Versions/14/lib/libpq.dylib) was built for newer macOS version (10.12) than being linked (10.9)
  dylib (/Applications/Postgres.app/Contents/Versions/14/lib/libcrypto.dylib) was built for newer macOS version (10.12) than being linked (10.9)

Here we can see that the lack of arm64e architecture in the libpq libraries is the root cause.

To fix this, specify that you only want arm64, as that's what's available in libpq.

ARCHFLAGS="-arch arm64" pip3 install psycopg2 --no-cache-dir --force-reinstall -v

If your warnings are different, then this advice won't apply but it should give you some clues as to what's happening. The warning about being built for a newer build of macOS shouldn't break things. Ignoring the libpq libraries should really be an error versus a warning here.

Positronium answered 16/9, 2022 at 4:12 Comment(0)
D
1

None of the above answer works for python 3.8 version. I had to upgrade to python 3.9 and the error went away. - I have 12.4 macOs with M1 PRO chip

Decare answered 22/7, 2022 at 0:48 Comment(0)
P
0

To those still struggling

Are you creating venv using PyCharm?

this was the problem in my case. I deleted venv created by pycharm and i created it manually with python3 -m venv /path/to/venv then activate and pip3 install -r requirements.txt runs perfectly

Pervert answered 24/6, 2022 at 10:33 Comment(0)
A
0

Had a similar issue when trying to install a Django project. psycopg2cffi wouldn't install, giving me almost the same error message.

pip install -r requirements.txt --use-pep517 Solved the problem.

Altarpiece answered 30/8, 2023 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.