在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个回答

0
我曾经遇到过这个问题。我的解决方法是确保Python版本与我使用的pip安装方式相匹配。这需要进行“pip3”安装。

0

由于我在安装macOS 11.2上的psycopg2时遇到了麻烦,所以我偶然发现了这个帖子,但是解决方案对我不再起作用,因为Homebrew的路径已经改变了。

对我来说,以下方法有效:

$ env LDFLAGS="-I/opt/homebrew/Cellar/openssl@1.1/1.1.1m/include -L/opt/homebrew/Cellar/openssl@1.1/1.1.1m/lib" pip install psycopg2

我认为在将来这些路径可能会再次更改;)


0
如果您正在尝试在已激活的conda环境中安装psycopg2,并且遇到了此错误,请运行conda install psycopg2而不是pip install psycopg2
在Monterey 12.1上对我有效。

0
% export PG_HOME=/Library/PostgreSQL/14

% export PATH=$PATH:$PG_HOME/bin 

% pip install psycopg2

这个会起作用。谢谢


目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何编写好答案的更多信息。 - Community

0

我将尝试总结所有答案。

我解决了问题,并在我的macOS Ventura 13.4上安装了psycopg2

不要使用psycopg2-binary!注意:不要在生产环境中使用psycopg2-binary,因为:

“二进制包在开发和测试中是一个实用的选择, 但���生产环境中建议使用从源代码构建的软件包。”

按照以下步骤进行操作:

如果您还没有安装,请安装postgresql

brew install postgresql

检查 pg_config 程序:如果您安装了 postgres,那么您应该有这个文件,它是通过安装 libpq-dev 而得到的。
pg_config --version

如果返回错误或意外的版本号,请找到 pg_config 文件:
find /usr/ -name pg_config

做以下事情:
export PATH=<path>:$PATH

例如:

export PATH=/usr/local//Cellar/postgresql@14/14.8_2/bin/:$PATH

如果错误再次出现,请检查回溯,并且如果您看到以下类似文本:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

然后运行以下命令:

xcode-select --install

这些步骤对我有效。

官方构建先决条件:https://www.psycopg.org/docs/install.html#build-prerequisites


-1
pip install psycopg binary

当我在终端安装时,这对我有用。花了我大约45分钟才弄清楚不要使用任何-或[],就像我看到的其他所有文档一样。


实际上...那不起作用。我必须去postgresapp.com,下载postgres。然后,从那里卸载我的python列表中的所有psycopg。接着,运行postgres。在终端中运行命令-> psql -U postgres <-。然后我在我的python IDE终端中运行命令-> sudo mkdir -p /ect/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /ect/paths.d/postgresapp <-。经过所有这些步骤,我能够在我的python终端中运行-> pip install psycopg2-binary<-,现在它正在运行。 - Americana American

-1
对于那些已经安装了PostgressApp但仍然遇到相同错误的人: 您需要按照设置文档中描述的第三步进行设置:https://postgresapp.com/ 运行这两个命令来设置您的PostgressApp路径。
sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

记得重新启动所有的终端窗口以使更改生效。 之后像往常一样运行。
pip install psycopg2

这次它应该按预期工作。

对我不起作用。我正在使用macOS Catalina(版本10.15.7)。 - Ged Flod

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