任何人都知道如何在 CentOS 6.5 上安装 psycopg2 with python 3.4



我在网上搜索了几个小时,结果没有任何效果。

我试图在Django项目上使用PostgreSQL,我在CentOS 6.5服务器上使用Python3.4。

但是我无法安装 psycopg2 库来让 PostgreSQL 工作。几乎所有我找到的文章都指指我在 CentOS 上使用 apt-get,但 apt-get 不是 CentOS 6.5 上的标准工具。我未能安装 apt-get,所以我无法安装 psycopg2。我还能做些什么来用Python 3.4安装psycopg2吗?

使用以下步骤:

1. yum install postgresql-server
2. yum install python-devel
3. service postgresql initdb
4. service postgresql start
5. yum install python-psycopg2

我刚刚在 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 程序,然后使用它来检索和安装 python34 的 psycopg2。 我在以下位置找到了有用的说明:

http://ask.xmodulo.com/install-python3-centos.html

我以根身份做了以下操作:

# 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 2.7的默认版本。在此之后,我终于能够通过以下方式构建并安装一个可供 python3 访问的 psycopg2 版本:

# pip3 install psycopg2

现在两个测试程序都可以工作了。

最新更新