我刚刚在 Centos 7.2 上处理过这个问题,也许我的结果会是
适用于你的情况。我是大约 40 台 Centos 机器的系统管理员,但是
我对 python 非常非常陌生。
更新 Centos 软件时,我的直觉是尽可能多地尝试
通过 yum 等系统实用程序,并在 yum 中尽可能使用标准存储库。我的经验是从其他来源安装和/或从源代码编译和安装可能会导致版本/兼容性问题。而且当你想使用python3时,你必须小心不要破坏Centos(例如yum)需要的第二版python。
但在这种情况下,我无法让 psycopg2 在两者下正常运行
系统 python (python 2.7) 和 python3.4 通过 yum。我必须使用 python 'pip' 安装实用程序,而且我首先必须在 yum 之外安装 pip 本身(具体来说是 pip3)。
我有以下 yum 存储库:
# yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.symnds.com
* epel: mirror.symnds.com
* extras: centos.mirror.nac.net
* updates: centos.mirror.nac.net
repo id repo name status
!base/7/x86_64 CentOS-7 - Base 9,007
!epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 10,681
!extras/7/x86_64 CentOS-7 - Extras 392
!updates/7/x86_64 CentOS-7 - Updates 2,529
repolist: 22,609
#
我安装了以下 python3 包:
# yum list installed | grep python3
python3-rpm-macros.noarch 3-10.el7 @epel
python34.x86_64 3.4.3-7.el7 @epel
python34-debug.x86_64 3.4.3-7.el7 @epel
python34-devel.x86_64 3.4.3-7.el7 @epel
python34-libs.x86_64 3.4.3-7.el7 @epel
python34-setuptools.noarch 19.2-3.el7 @epel
python34-setuptools_scm.noarch 1.10.1-2.el7 @epel
python34-test.x86_64 3.4.3-7.el7 @epel
python34-tkinter.x86_64 3.4.3-7.el7 @epel
python34-tools.x86_64 3.4.3-7.el7 @epel
#
常规 python 包的列表要长得多(包括 'python-' 版本
以上所有python3包)。它包括三个 psycopg2 包:
# yum list installed | grep psycopg2
python-psycopg2.x86_64 2.5.1-3.el7 @base
python-psycopg2-debug.x86_64 2.5.1-3.el7 @base
python-psycopg2-doc.x86_64 2.5.1-3.el7 @base
#
没有名为 python3-psycopg2 的包。这样做之后,我能够
在 python 程序中打开 psycopg2 包,但不在任何 python34 程序中。在那里我总是会收到一条错误消息,告诉我没有 psycopg2 模块。这两个测试程序中的第一个可以工作,但第二个不行:
$ cat tpy1.py
#!/usr/bin/python
import sys
print sys.path
import psycopg2
try:
conn = psycopg2.connect("dbname='dev' user='i2' host='localhost' password='pp'" )
except:
print( "Cannot connect to dev database on localhost" )
conn.close()
$ cat tpy1.py3
#!/usr/bin/python3
import sys
print( sys.path )
import psycopg2
try:
conn = psycopg2.connect("dbname='dev' user='i2' host='localhost' password='pp'" )
except:
print( "Cannot connect to dev database on localhost" )
conn.close()
$
为了让 python34 可以访问 psycopg2,我必须先安装 pip3 程序,然后使用它来检索和安装 psycopg2 for python34。我在以下位置找到了有用的说明:
http://ask.xmodulo.com/install-python3-centos.html
我以 root 身份执行了以下操作:
# curl -O https://bootstrap.pypa.io/get-pip.py
# /usr/bin/python3.4 get-pip.py
# /usr/bin/python get-pip.py
这给了我以下版本的“pip”:
# ls -l /usr/bin/pip*
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip2
-rwxr-xr-x 1 root root 204 Oct 13 15:59 /usr/bin/pip2.7
-rwxr-xr-x 1 root root 207 Oct 13 15:33 /usr/bin/pip3
-rwxr-xr-x 1 root root 207 Oct 13 15:33 /usr/bin/pip3.4
#
'pip' 的默认版本可能最好是系统 python 的版本,python 2.7。在此之后,我终于能够通过以下方式构建和安装可供 python3 访问的 psycopg2 版本:
# pip3 install psycopg2
现在两个测试程序都可以工作了。