Mac 下 pip3 install psycopg2 出现的错误和解决

Mac 上配置开发环境时遇到的问题,留个记录。


1、Error: pg_config executable 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'.
    Complete output from command python setup.py egg_info:
    running egg_info
    ...
    ...

解决方法较为简单,安装下 PostgreSQL 或者 PostgreSQL 的开发环境即可:

brew install postgresql

没有 brew 的先安装:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

brew 在 GitHub 上的项目仓库:Homebrew/brew

之后记得重新开一个终端以加载新的环境变量!

2、error: command ‘clang’ failed with exit status 1

具体错误为:

...
...
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
...
...

解决方法是安装 [email protected]

# 安装 [email protected]
brew reinstall openssl

如果你之前用其他工具安装过的话,只需要将其目录添加至环境变量中即可:

export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"

具体参照这篇讨论:Can’t install psycopg2 package through pip install on MacOS

同样之后你需要重新打开一个终端。

结束。