这可能是底层系统中的缺陷问题。可能会有多个版本的共享库,在运行时您可能无法找到正确的库。通常很难解决。由于我也遇到了同样的问题,我将尝试找出问题的原因。这将比提供固定的解决方案更有用。我仍需要为自己找到解决方案:
1. 从更新的版本构建libpq库。
2. 找到编译psycopg2时使用的适当的libpq。
进入psycopg2的源目录:
myuid@mycomputer:~/Downloads/psycopg2$ find . -name "*.c" | xargs grep PQconninfo
./psycopg/psycopgmodule.c: PQconninfoOption *options = NULL;
./psycopg/psycopgmodule.c: options = PQconninfoParse(Bytes_AS_STRING(dsn), &err);
./psycopg/psycopgmodule.c: PyErr_SetString(OperationalError, "PQconninfoParse() failed");
./psycopg/psycopgmodule.c: PQconninfoFree(options);
./psycopg/connection_int.c: PQconninfoOption *connopts, *ptr;
./psycopg/connection_int.c: connopts = PQconninfoParse(pgdsn, NULL);
./psycopg/connection_int.c: PQconninfoFree(connopts);
./psycopg/connection_int.c: PQconninfoOption *options = NULL;
./psycopg/connection_int.c: if (!(options = PQconninfoParse(dsn, NULL))) {
./psycopg/connection_int.c: PQconninfoFree(options);
./psycopg/conninfo_type.c:".. seealso:: libpq docs for `PQconninfo()`__ for details.\n"
./psycopg/conninfo_type.c: PQconninfoOption *options = NULL;
./psycopg/conninfo_type.c: if (!(options = PQconninfo(self->conn->pgconn))) {
./psycopg/conninfo_type.c: PQconninfoFree(options);
./psycopg/conninfo_type.c: PyErr_SetString(NotSupportedError, "PQconninfo not available in libpq < 9.3");
./psycopg/connection_type.c: PQconninfoOption *options = NULL;
./psycopg/connection_type.c: if (!(options = PQconninfo(self->pgconn))) {
./psycopg/connection_type.c: PQconninfoFree(options);
./psycopg/connection_type.c: PyErr_SetString(NotSupportedError, "PQconninfo not available in libpq < 9.3");
./psycopg/utils.c:
./psycopg/utils.c:psyco_dict_from_conninfo_options(PQconninfoOption *options, int include_password)
./psycopg/utils.c: PQconninfoOption *o;
注意:psycopg的作者已经通过该行给出了答案。
"PQconninfo not available in libpq < 9.3"
这意味着问题出在运行时较旧的 libpq 上,否则你可能使用了更新版本的 libpq,否则 psycopg2 可能无法编译。
通过行代码:PQconninfo(self->conn->pgconn),你可以知道 PQconninfo 是一个 C 函数,应该在你的 libpq 库中存在。
错误信息通常会告诉你问题所在,例如我的情况是:
/usr/local/lib/python3.8/site-packages/psycopg2-2.8.5.dev0-py3.8-linux-x86_64.egg/psycopg2
Full error: >>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/site-packages/psycopg2-2.8.5.dev0-py3.8-linux-x86_64.egg/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import (
ImportError: /usr/local/lib/python3.8/site-packages/psycopg2-2.8.5.dev0-py3.8-linux-x86_64.egg/psycopg2/_psycopg.cpython-38-x86_64-linux-gnu.so: undefined symbol: PQconninfo
在这里,您应该找到一个共享库:
_psycopg.cpython-38-x86_64-linux-gnu.so
请看这个库:
ldd _psycopg.cpython-38-x86_64-linux-gnu.so
linux-vdso.so.1 => (0x00007ffe985c7000)
libpq.so.5 => /usr/lib64/libpq.so.5 (0x00007ff922a13000)
libpthread.so.0 => /usr/lib64/libpthread.so.0 (0x00007ff9227f7000)
libc.so.6 => /usr/lib64/libc.so.6 (0x00007ff922429000)
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007ff9221b7000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007ff921d54000)
libkrb5.so.3 => /usr/lib64/libkrb5.so.3 (0x00007ff921a6b000)
libcom_err.so.2 => /usr/lib64/libcom_err.so.2 (0x00007ff921867000)
libgssapi_krb5.so.2 => /usr/lib64/libgssapi_krb5.so.2 (0x00007ff92161a000)
libldap_r-2.4.so.2 => /usr/lib64/libldap_r-2.4.so.2 (0x00007ff9213bb000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff922e87000)
libk5crypto.so.3 => /usr/lib64/libk5crypto.so.3 (0x00007ff921188000)
libdl.so.2 => /usr/lib64/libdl.so.2 (0x00007ff920f84000)
libz.so.1 => /usr/lib64/libz.so.1 (0x00007ff920d6e000)
libkrb5support.so.0 => /usr/lib64/libkrb5support.so.0 (0x00007ff920b5e000)
libkeyutils.so.1 => /usr/lib64/libkeyutils.so.1 (0x00007ff92095a000)
libresolv.so.2 => /usr/lib64/libresolv.so.2 (0x00007ff920741000)
liblber-2.4.so.2 => /usr/lib64/liblber-2.4.so.2 (0x00007ff920532000)
libsasl2.so.3 => /usr/lib64/libsasl2.so.3 (0x00007ff920315000)
libssl3.so => /usr/lib64/libssl3.so (0x00007ff9200bc000)
libsmime3.so => /usr/lib64/libsmime3.so (0x00007ff91fe94000)
libnss3.so => /usr/lib64/libnss3.so (0x00007ff91fb65000)
libnssutil3.so => /usr/lib64/libnssutil3.so (0x00007ff91f935000)
libplds4.so => /usr/lib64/libplds4.so (0x00007ff91f731000)
libplc4.so => /usr/lib64/libplc4.so (0x00007ff91f52c000)
libnspr4.so => /usr/lib64/libnspr4.so (0x00007ff91f2ee000)
libselinux.so.1 => /usr/lib64/libselinux.so.1 (0x00007ff91f0c7000)
libcrypt.so.1 => /usr/lib64/libcrypt.so.1 (0x00007ff91ee90000)
librt.so.1 => /usr/lib64/librt.so.1 (0x00007ff91ec88000)
libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007ff91ea26000)
libfreebl3.so => /usr/lib64/libfreebl3.so (0x00007ff91e823000)
注意这一行:libpq.so.5 => /usr/lib64/libpq.so.5
现在只关注这个库中的PPconninfo(它有很多函数和数据)
nm -D /usr/lib64/libpq.so.5 | grep PQconninfo
000000000000cb90 T PQconninfoFree
000000000000df30 T PQconninfoParse
看起来你没有PQconninfo函数。
我认为通过说明如何解决这个问题会更有帮助,这可能会有助于你解决许多类似的问题。
现在我将在我的系统上添加解决方案:
结果发现我的系统上有多个libpg。
cd /usr
find . -name "libpq*"
... many others ignored
./pgsql-11/lib/libpq.so
然后将LD_LIBRARY_PATH更新为/usr/pgsql-11/lib:/usr/local/lib64,问题就解决了。
python3.8
Python 3.8.1 (default, Jan 7 2020, 15:07:37)
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2