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

Mac Anaconda环境运行pytest时pg_config找不到问题求助

解决Anaconda环境中pytest-postgresql找不到pg_config的问题

问题根源

Anaconda环境启动时会重置PATH环境变量,优先使用conda自身的bin目录,导致系统shell中配置的Homebrew路径(/opt/homebrew/bin)没有被包含到conda环境的PATH里。而pytest-postgresql的fixture通过subprocess调用pg_config时,继承的是conda环境的PATH,因此找不到目标文件。

解决方案

1. 临时修复(单次生效)

激活conda环境后,先手动将Homebrew的bin目录添加到PATH,再运行pytest:

conda activate your_env_name
export PATH="/opt/homebrew/bin:$PATH"
pytest

2. 永久修复(conda环境全局生效)

修改conda环境的激活脚本,让每次激活环境时自动添加Homebrew路径:

  • 激活目标conda环境:
    conda activate your_env_name
    
  • 创建环境的激活脚本目录:
    mkdir -p $CONDA_PREFIX/etc/conda/activate.d
    
  • 在该目录下创建env_vars.sh文件,写入PATH配置:
    echo 'export PATH="/opt/homebrew/bin:$PATH"' > $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh
    
  • 重新激活环境后,配置永久生效:
    conda deactivate
    conda activate your_env_name
    

3. 直接指定pg_config路径(不修改环境变量)

在测试代码的conftest.py中重写postgresql fixture,直接传入pg_config的绝对路径:

from pytest_postgresql.factories import postgresql as _postgresql

# 重写fixture,指定pg_config的绝对路径
postgresql = _postgresql(
    pg_config="/opt/homebrew/bin/pg_config"
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 05:00:16