在MacOS 10.9.5上安装Psycopg2出现错误

100

我正在尝试在我的Macbook上安装Psycopg2,但是出现了错误。我在StackOverflow上找到了很多相同的问题,但没有一个答案似乎起作用。
我正在使用:

操作系统:MacOS 10.9.5
Python版本:3.4.3

我的错误代码是:

Running setup.py egg_info for package psycopg2  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

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
 
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 in
/Users/sg/build/psycopg2 Storing complete log in
/Users/sg/Library/Logs/pip.log

1
https://dev59.com/R2gu5IYBdhLWcg3wDS4G#24645416 - madzohan
27个回答

4
在Mac电脑上未安装PostgreSQL数据库,而psycopg2正在寻找Postgres文件。只需安装Postgres数据库并重新运行pip install,它将正常工作。
从上面的链接中下载Postgres。 https://postgresapp.com/downloads.html 将其拖放到应用程序文件夹中。 初始化数据库,最后一步是配置您的路径。 sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp 这将要求您输入Mac密码。
您可以将上述链接复制粘贴到终端中并按Enter键。 有关此链接来自何处的更多信息,请查看下面的链接: https://postgresapp.com/ 重新启动终端。
要测试,请写入以下命令: psql -U postgres
该命令将提示您进入Postgres终端。
完成所有操作后,请尝试重新安装psycopg-2,它将正常工作。

4
抱歉,我没有理解您的问题。请问您需要什么帮助?
brew install postgresql
pip install psycopg2

为什么会发生这种情况?

这个问题主要出现在搭载苹果芯片(M1)的MacBook电脑上。

解决方法是使用brew在Mac上安装Postgres。psycopg2是与Postgres相关的库,因此安装完整版本的Postgres会同时安装缺失的库。

尝试执行以下命令:

brew install postgresql

这将在你的MacBook本地安装一个Postgres服务器。
如果出现错误,可能是因为你没有安装brew。我强烈建议所有的Mac用户安装它。
检查你是否已经安装了brew:
brew -v 

如果这不起作用,请安装brew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

在使用brew安装postgresql后,再次使用pip重新安装库,就像您之前尝试过的那样。
pip install psycopg2

也许这个问题将来会得到解决,当苹果芯片的支持增加时,默认的 pip 安装程序将可以正常工作。


3

即使我使用的是MacOx 10.15.6,也无法安装psycopg2。

我尝试了pip install psycopg2-binary,它起作用了。


2
安装OpenSSL并运行:export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/。该命令导出其路径。

2
我遇到了“ld:未找到-lssl库”的错误,这个方法解决了问题。 - Jamesst20
2
我使用Homebrew安装了OpenSSL,即brew install openssl。然后我需要运行export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib",这是我从运行brew info openssl得到的。然后pip3 install psycopg2-binary --user就可以工作了。 - Karl Penzhorn

2

首先运行brew install postgresql,然后运行pip install psycopg2


1
如果您已经从官方安装程序安装了PostgreSQL,但在通过venv创建的虚拟环境中使用pip安装psycopg2时仍然出现此错误,请按照以下步骤操作:
  • 打开终端(这是在虚拟环境之外),使用命令找到pg_config的位置:sudo find / -name pg_config
  • 一旦您从输出列表中找到位置,请从虚拟环境内部添加到PATH:export PATH={location}:$PATH(确保您将“{location}”替换为您发现的文件夹)
  • 然后在虚拟环境中继续安装psycopg2:pip install psycopg2

1

在Mac上安装psycopg2,请按照以下步骤进行

  1. 前往应用程序文件夹
  2. 选择Postgre.app
  3. 右键单击并选择“显示包内容”
  4. 选择Content-->Versions-->{版本号}-->bin
  5. 检查版本号

export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/{Version Number}/bin/

例子
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/12/bin/

然后运行以下代码。
pip3 install psycopg2

这应该可以解决你的问题。

0

在我的Big Sur上遇到了同样的问题,在我的情况下,我已经安装了PostgreSQL。不知何故,我重启了机器并尝试安装psycopg2,然后它就可以正常工作了。


0
在我的情况下,错误详情如下:
#error architecture not supported ^ fatal error: too many errors emitted, stopping now [-ferror-limit=] 20 errors generated.
我告诉pip我正在使用的架构,现在psycopg2可以顺利安装。
ARCHFLAGS="-arch x86_64" pip3 install nltk

0
  • PATH=$PATH:/Library/PostgreSQL/13/bin #路径是你的postgresql安装路径。通常在 Macintosh HD - lIBRARY - POSTGRESSQL/INSTALLED VERSION/BIN 内
  • pip3 install --upgrade wheel
  • pip3 install --upgrade setuptools
  • pip3 install psycopg2
  • 我从"https://www.youtube.com/watch?v=N4RxnQH2pVY"获得了这些步骤。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接