【问题标题】:How to install psycopg2 with pg_config error?如何使用 pg_config 错误安装 psycopg2?
【发布时间】:2016-05-08 08:15:03
【问题描述】:

我尝试从this site 安装 psycopg2(PostgreSQL 数据库适配器),但是当我在 cd 进入包并写入后尝试安装时

python setup.py install 

我收到以下错误:

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'.

我也尝试过“sudo pip install psycopg2”,得到了同样的信息。

阅读docs后,它要求查看setup.cfg文件(如下):

[build_ext]
define=

# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information

# "pg_config" is required to locate PostgreSQL headers and libraries needed to
# build psycopg2. If pg_config is not in the path or is installed under a
# different name uncomment the following option and set it to the pg_config
# full path.
#pg_config=

# Set to 1 to use Python datetime objects for default date/time representation.
use_pydatetime=1

# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=

# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0

# Statically link against the postgresql client library.
#static_libpq=1

# Add here eventual extra libraries required to link the module.
#libraries=

但是,我不确定是否要编辑此文件,因为文档说明如下:

then take a look at the setup.cfg file.

Some of the options available in setup.cfg are also available as command line arguments of the build_ext sub-command. For instance you can specify an alternate pg_config version using:

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

Use python setup.py build_ext --help to get a list of the options supported.

我已经获得了支持的选项列表,但我不确定从那里去哪里

【问题讨论】:

    标签: python postgresql psycopg2


    【解决方案1】:

    在 Fedora 上 (在 Fedora 35 上测试并从源代码构建)

    pg_config 存在于libpq5-devel 中。 现在尝试以下步骤:

    sudo dnf install libpq5-devel
    
    python setup.py build
    sudo python setup.py install
    

    【讨论】:

      【解决方案2】:

      在带有 M1 的 Macbook 上,我必须安装 postgresql

      brew install postgresql
      

      (如果你没有 brew:https://brew.sh

      然后再次运行安装

      python3 -m pip install psycopg2-binary
      

      【讨论】:

        【解决方案3】:

        如果您使用的是 Ubuntu 或任何其他基于 debian 的发行版,请尝试

        sudo apt-get install python3-psycopg2
        

        否则,您需要为您的发行版查找并安装 Postgresql 客户端软件包。 psycopg2 installation from source

        【讨论】:

        • 有了这个,你怎么能用requirements.txt标准化安装?
        • 我在 Ubuntu Buster 上,安装了这个 python-psycopg2 包,仍然没有工作。顺便说一句,这个包是针对 python2.x 的,对于 python 3.x,包名是python3-psycopg2。也没有骰子。我正在运行 python 3.8
        • 这几乎肯定是解决问题的错误方法。你永远不应该以 root 身份安装 pip 包,除了 docker 容器并且你知道自己在做什么,即便如此,最好还是创建一个进程用户。
        【解决方案4】:

        对于带有 Macports 的 OSX,您可以为 Python 3.8 安装 sudo port install py38-psycopg2。您可以使用 port search psycopg2 搜索您的 Python 版本。在撰写本文时,版本范围从 2.7 到 3.9,都与 psycopg2 的 2.8.6 版本保持同步。

        注意:这可能对 venv 没有帮助。

        编辑:所以要找到 venv 的 pg_config,运行命令 port contents postgresql13 | grep pg_config 其中postgresql13 是从上述端口命令安装的 postgresql 包的版本。您正在寻找诸如/opt/local/lib/postgresql13/bin/pg_config 之类的路径。使用 export PATH=/opt/local/lib/postgresql13/bin/:$PATH 之类的命令将其导出到 PATH 变量。

        请参阅 this answer 了解 Homebrew。

        【讨论】:

          【解决方案5】:
          Debian/Ubuntu
          

          Python 2

          sudo apt install libpq-dev python-dev
          

          Python 3

          sudo apt install libpq-dev python3-dev
          

          附加

          If none of the above solve your issue, try
          
          sudo apt install build-essential
          or
          
          sudo apt install postgresql-server-dev-all
          

          用点子

          改为安装psycopg2-binary PyPI 包,它具有适用于 Linux 和 Mac OS 的 Python 轮子。

          pip install psycopg2-binary
          

          【讨论】:

          • 我也有这个问题。可悲的是,我的软件包 apt 安装 python3-dev 将无法正常工作。我所做的是对这个答案的排列:1.sudo pip install --upgrade pip 2.sudo apt install -y libpq-dev postgresql-server-dev-all 3.sudo pip3 install psycopg2 不确定是否所有sudo 都是必要的,但我不在乎,所以我把它们放了。
          • 对于虚拟环境pip install psycopg2-binary 是解决方案
          【解决方案6】:

          如果需要不编译安装:

          pip install psycopg2-binary
          

          https://www.psycopg.org/docs/install.html#binary-install-from-pypi

          注意: psycopg2-binary 包是为初学者准备的 无需满足构建即可玩 Python 和 PostgreSQL 要求。如果您是发布包的维护者,具体取决于 在 psycopg2 上,您不应该使用“psycopg2-binary”作为模块 依赖。对于生产用途,建议您使用源代码分发。

          【讨论】:

          • 干净快速的解决方案。它不需要任何其他库安装。谢谢。
          • 来自 psycopg2 (psycopg.org/docs/install.html#binary-install-from-pypi) 的文档:注意 psycopg2-binary 包是为了让初学者开始使用 Python 和 PostgreSQL 而无需满足构建要求。如果您是依赖 psycopg2 的发布包的维护者,则不应使用“psycopg2-binary”作为模块依赖项。对于生产用途,建议您使用源代码分发。请将此添加到您的答案中。
          【解决方案7】:

          我遇到这个问题是因为我还没有在我的机器上安装 PostgreSQL。在 mac 上,只需一个简单的 brew install postgresql 即可解决问题。

          【讨论】:

          • 是的,它也是。
          【解决方案8】:

          升级 pip 对我有用:pip install --upgrade pip

          【讨论】:

            【解决方案9】:

            对于像我这样从源代码构建 postgrespsycopg2 的人,这里有另一个解决方案:

            sudo su
            export PATH=/usr/local/pgsql/bin:$PATH #or path to your pg_config
            

            现在来自 psycopg2 的 setup.py 可以正确找到 pg_config。

            python3 setup.py install
            

            或者如果您只想使用pip3pip3 install psycopg2 也应该可以。

            【讨论】:

              猜你喜欢
              • 2017-06-04
              • 2016-12-19
              • 1970-01-01
              • 2020-05-20
              • 2018-03-24
              • 1970-01-01
              相关资源
              最近更新 更多