未定义符号:PQconninfo

时间:2019-07-10 21:14:50

标签: python-3.5 psycopg2 postgresql-9.5

无法导入psycopg2

在python控制台中的输出:

  

导入psycopg2

     

回溯(最近通话最近):   文件“”,第1行,位于    

中的文件“ /home/user/.py_virtualenvs/verb_py3/lib/python3.5/site-packages/psycopg2/init.py”,第50行      从psycopg2._psycopg导入

(#noqa   ImportError:/home/user/.py_virtualenvs/verb_py3/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-x86_64-linux-gnu.so:未定义符号:PQconninfo

4 个答案:

答案 0 :(得分:3)

这可能是基础系统中的缺陷问题。共享库可能有多个版本,并且在运行时您可能找不到正确的库。解决起来通常很痛苦。由于我遇到了同样的问题,因此我将尝试解决问题。这将比给出固定的解决方案有用。我仍然需要为自己找到解决方案: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);    /* safe on null */
./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:/* Make a dict out of PQconninfoOption array */
./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 (                     # noqa
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

答案 1 :(得分:1)

使用conda而不是pip重新安装psycopg2解决了该问题。

答案 2 :(得分:0)

安装了psycopg2-binary软件包解决了我的问题。

pip install psycopg2-binary

答案 3 :(得分:0)

就像@Kemin Zhou在一个答案中说的那样,导致此问题的原因之一可能是年龄更大的libpq.so file

我当前正在使用Cent OS 7,安装的postgresql-lib版本为:

$ yum list installed | grep postgres
Failed to set locale, defaulting to C
postgresql.x86_64                    9.2.24-4.el7_8                   installed
postgresql-devel.x86_64              9.2.24-4.el7_8                   installed
postgresql-libs.x86_64               9.2.24-4.el7_8                   installed

我什至无法更新它,因为yum找不到它的更高版本:

$ yum list --showduplicates postgresql
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * epel: mirrors.ircam.fr
Installed Packages
postgresql.x86_64 9.2.24-4.el7_8 installed
Available Packages
postgresql.i686                    9.2.24-2.el7                    base
postgresql.x86_64                  9.2.24-2.el7                    base
postgresql.i686                    9.2.24-2.el7_7                    updates
postgresql.x86_64                  9.2.24-2.el7_7                    updates
postgresql.i686                    9.2.24-4.el7_8                    updates
postgresql.x86_64                  9.2.24-4.el7_8                    updates

This链接帮助了我。

使用该链接,我首先添加了此仓库,其中包含更新的postgresql10-libs:

$ yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

然后我安装了更新的postgresql10-libs

$ yum install postgresql10-libs.x86_64