You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac M2 Monterey环境下Django项目安装psycopg2报错解决

Mac M2 Monterey 安装 psycopg2 报错的解决方法

近期迁移至Mac M2 Monterey系统,搭建Django项目开发环境时,安装psycopg2遇到报错,核心问题是找不到pg_config可执行文件,尝试安装旧版本也失败,具体报错信息如下:

Using cached psycopg2-2.8.5.tar.gz (380 kB)
ERROR: Command errored out with exit status 1:
 command: /opt/homebrew/opt/python@3.9/bin/python3.9 -c 'import sys, setuptools, tokenize; sys.argv[0] = "'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/setup.py'"'"; __file__="'"'/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/setup.py'"'";f=getattr(tokenize, "'"'open'"'", open)(__file__);code=f.read().replace("'"'\r\n'"'", "'"'\n'"'");f.close();exec(compile(code, __file__, "'"'exec'"'"))' egg_info --egg-base /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r
     cwd: /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-install-9wiqe2nr/psycopg2_97efcf6747c249769acdc8430ba4238f/
Complete output (23 lines):
running egg_info
creating /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info
writing /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/PKG-INFO
writing dependency_links to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/dependency_links.txt
writing top-level names to /private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/top_level.txt
writing manifest file '/private/var/folders/m5/vhnyyjg16gd352x6wdhpjysh0000gn/T/pip-pip-egg-info-ap2am61r/psycopg2.egg-info/SOURCES.txt'

Error: pg_config executable not found.

pg_config is required to build psycopg2 from source.  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'.

If you prefer to avoid building psycopg2 from source, please install the PyPI
'psycopg2-binary' package instead.

For further information please check the 'doc/src/install.rst' file (also at
<https://www.psycopg.org/docs/install.html>).
==============================================================================
Its beginning of the error, After that its tried to install each previous version of Psycopg 
==============================================================================

有效解决方法

  • 方法一:安装PostgreSQL(推荐)
    通过Homebrew安装PostgreSQL,它会自动附带pg_config:

    brew install postgresql
    

    安装完成后重启终端,再执行pip install psycopg2即可完成安装。

  • 方法二:指定pg_config路径(已安装PostgreSQL但路径未配置)
    先定位pg_config的位置,M2芯片下Homebrew安装的路径通常为/opt/homebrew/bin/pg_config,然后执行:

    pip install psycopg2 --global-option=build_ext --global-option="--pg-config=/opt/homebrew/bin/pg_config"
    
  • 方法三:安装预编译二进制包(无需源码编译)
    若不想编译源码,直接安装预编译的psycopg2-binary包:

    pip install psycopg2-binary
    

    注意:该包更适合开发环境,生产环境建议使用标准的psycopg2包。

内容的提问来源于stack exchange,提问作者sujant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 05:54:22