简体   繁体   中英

Psycopg2 error: Symbol not found _PQbackendPID

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?

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

  • pip uninstall psycopg2
  • pip install psycopg2-binary

我遇到了同样的问题,使用选项--no-cache-dir安装psycopg2-binary对我有帮助。

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM