【问题标题】:Anyone knows how to install psycopg2 with python 3.4 on CentOS 6.5?任何人都知道如何在 CentOS 6.5 上使用 python 3.4 安装 psycopg2?
【发布时间】:2015-02-05 08:52:46
【问题描述】:

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

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

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

【问题讨论】:

标签: python django postgresql centos psycopg2


【解决方案1】:

使用以下步骤:

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

【讨论】:

    【解决方案2】:

    我刚刚在 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
    

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

    【讨论】:

    • 经过反思,如果默认的 'pip' 是 python3 版本可能会更好。我认为可以信任 Centos 可以通过 yum 维护它使用的 python (python2)。我(只是稍微了解一下)的感觉是,大多数活跃的 python 开发都使用 python 的第三版,使用 pip、virtualenv 等 python 工具的人更有可能使用 python3。再说一次,我对 python 很陌生,我不知道哪个版本对应用程序开发人员来说是最好的默认设置;欢迎提出意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 2014-06-06
    • 2014-05-22
    • 2014-06-25
    • 2016-09-17
    • 2016-08-20
    相关资源
    最近更新 更多