Psycopg2 安装,不被 Python 读取 - OS 10.8.4、Python 2.7、PostgreSQL 9.2

Posted

技术标签:

【中文标题】Psycopg2 安装,不被 Python 读取 - OS 10.8.4、Python 2.7、PostgreSQL 9.2【英文标题】:Psycopg2 Installing, Not Being Read by Python - OS 10.8.4, Python 2.7, PostgreSQL 9.2 【发布时间】:2013-06-11 14:02:16 【问题描述】:

操作系统 10.8.4、Python 2.7、PostgreSQL 9.2

过去 10 多个小时我一直在尝试为 Django 安装 Psycopg2,现在收到警告和 Google 似乎无法帮助我解决的故障:

在清除所有现有的 Psycopg2 和 PostgreSQL 文件,重新安装 PostgreSQL 9.2 和 Python 2.7 后,我在尝试使用 Pip 安装 Psycopg2 时仍然收到 _PQbackendPID 错误。所以我尝试在安装时更改 ARCHFLAGS 设置:

$ sudo env ARCHFLAGS="-arch i386 -arch x86_64" pip install psycopg2

并收到多次重复的错误和警告:

warning: no files found matching '*.py' under directory 'ZPsycopgDA'
warning: no files found matching '*.gif' under directory 'ZPsycopgDA'
warning: no files found matching '*.dtml' under directory 'ZPsycopgDA'
warning: no files found matching '*' under directory 'psycopg2da'
warning: no files found matching '*' under directory 'debian'
no previously-included directories found matching 'doc/src/_build'
warning: no files found matching 'ChangeLog'

还有这个:

 clang: warning: argument unused during compilation: '-mno-fused-madd'

还有这个:

  psycopg/pqpath.c:1177:17: warning: array index of '3' indexes past the end of an array (that contains 1 elements) [-Warray-bounds] PyTuple_SET_ITEM(dtitem, 3, tmp);

(最后一条消息出现在索引 1-6 中)

还有这个:

./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function]

还有这个:

    psycopg/diagnostics_type.c:64:54: warning: implicit conversion loses integer precision: 'Py_intptr_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
 errortext = PQresultErrorField(self->err->pgres, (Py_intptr_t) closure);

最后,我明白了:

Successfully installed psycopg2

但是,当我进入 Python 时:

~/ $ python2.7
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named psycopg
>>> import psycopg2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
    from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Python/2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
  Referenced from: /Library/Python/2.7/site-packages/psycopg2/_psycopg.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/psycopg2/_psycopg.so

我们的老朋友 _PQbackendPID 又回来了,我解决问题的资源已经用完了。

【问题讨论】:

【参考方案1】:

在我看来,_psycopg.so 在运行时链接到的 libpq 与编译时所针对的 libpq 不同。

检查您的DYLIB_LIBRARY_PATH。还可以使用install_name_tool 来查看_psycopg.so 是否有@relative、不合格或绝对路径到libpq.dylib

【讨论】:

以上是关于Psycopg2 安装,不被 Python 读取 - OS 10.8.4、Python 2.7、PostgreSQL 9.2的主要内容,如果未能解决你的问题,请参考以下文章

安装 psycopg2 osx 10.12 python 3.5.2 - psycopg2 的构建轮失败

如何在 Python 上使用“pip”安装 psycopg2?

在 unix 上为 python 3.5 安装 psycopg2 包

python无网安装psycopg2

python 安装PostgreSQL 模块:psycopg2

centos python3 安装psycopg2 模块