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

MacOS Ventura下Python3.11.4的BeautifulSoup与html5lib报错求助

问题解决步骤

错误原因分析

  • AttributeError: module 'collections' has no attribute 'Callable':Python 3.10+ 已将collections.Callable迁移至collections.abc.Callable,部分旧版本依赖包仍在调用旧API导致报错。
  • AttributeError: module 'html5lib.treebuilders' has no attribute '_base':html5lib 1.1与Python 3.11存在兼容性问题,而降级至1.0b8版本又过于老旧,会触发前者的API报错。

具体解决步骤

1. 确认Python环境路径(排查MacOS自带Python冲突)

运行以下命令确认当前使用的Python是否为你安装的3.11.4版本:

which python3
python3 --version
  • 若输出路径为/usr/bin/python3,说明使用的是系统自带Python,建议优先使用自行安装的版本(如brew安装的/opt/homebrew/bin/python3或/usr/local/bin/python3),可通过调整PATH环境变量实现:
    echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
    

2. 安装兼容的依赖版本组合

卸载现有冲突依赖,安装经过验证的兼容版本:

pip3 uninstall -y beautifulsoup4 html5lib lxml
pip3 install beautifulsoup4==4.12.2 html5lib==1.0.1 lxml==4.9.3

3. 用虚拟环境隔离依赖(推荐)

全局环境易出现依赖冲突,创建虚拟环境可彻底隔离:

# 创建虚拟环境
python3 -m venv venv
# 激活虚拟环境(zsh/bash)
source venv/bin/activate
# 在虚拟环境内安装依赖
pip install beautifulsoup4==4.12.2 html5lib==1.0.1 lxml==4.9.3

激活虚拟环境后运行你的代码即可。

4. 关于重装Python

  • 优先完成上述步骤排查,若确认是Python安装文件损坏(如运行python3 -c "import collections.abc"报错),再考虑重装:
    # 若用brew安装
    brew reinstall python@3.11
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 17:23:17