如何在Python上安装带有“pip”的psycopg2?

  • 问题:
  • 我使用的是virtualenv,需要安装“psycopg2”

    我做了以下工作:

    pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160

    我有以下信息:

    Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
    -2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
    Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
    Running setup.py egg_info for package from http://pypi.python.org/packages/sou
    rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
    Error: pg_config executable not found.

    Please add the directory containing pg_config to the PATH
    or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.
    Complete output from command python setup.py egg_info:
    running egg_info

    creating pip-egg-info\psycopg2.egg-info

    writing pip-egg-info\psycopg2.egg-info\PKG-INFO

    writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt

    writing dependency_links to pip-egg-info\psycopg2.egg-info\dependency_links.txt

    writing manifest file 'pip-egg-info\psycopg2.egg-info\SOURCES.txt'

    warning: manifest_maker: standard file '-c' not found

    Error: pg_config executable not found.



    Please add the directory containing pg_config to the PATH

    or specify the full executable path with the option:



    python setup.py build_ext --pg-config /path/to/pg_config build ...



    or with the pg_config option in 'setup.cfg'.

    ----------------------------------------
    Command python setup.py egg_info failed with error code 1
    Storing complete log in C:\Documents and Settings\anlopes\Application Data\pip\p
    ip.log

    我的问题是,我只需要这么做就可以让心理医生工作?在

    python setup.py build_ext --pg-config /path/to/pg_config build ...

  • 答案:
  • 注意:很久以前,PyPI中就有针对Windows的二进制控制盘,因此对于Windows用户来说,这应该不再是一个问题。以下是针对Linux、Mac用户的解决方案,因为他们中的许多人都是通过网络搜索找到这篇文章的

    安装psycopg2 binaryPyPI包,它有针对Linux和macos的pythonwheels

    pip install psycopg2-binary

    从源代码安装构建psycopg2包的prerequsisites:

    sudo apt install libpq-dev python3-dev

    您可能需要安装python3.8-dev或类似软件,例如python3.8

    sudo apt install libpq-dev python-dev

    如果这还不够,试试看

    sudo apt install build-essential

    或者

    sudo apt install postgresql-server-dev-all

    在再次安装psycopg2之前

    看到了吗Banjer’s answer

    1真的吗?现在是2020年