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

使用Poetry在Python 3.12中安装pygame时遇报错求助

Python 3.12下用Poetry安装pygame报错的解决办法

问题场景

  • 环境:Python 3.12,使用Poetry管理项目依赖
  • pyproject.toml依赖配置:
[tool.poetry.dependencies]
python = "^3.12"
pygame = "^2.5.2"
  • 执行poetry install后触发错误:
...
    File "/Users/r_2009/Library/Caches/pypoetry/virtualenvs/coffeeshop-It7qMoV3-py3.12/lib/python3.12/site-packages/pip/_internal/metadata/importlib/_envs.py", line 111, in _find_eggs_in_dir
      from pip._vendor.pkg_resources import find_distributions
    File "/Users/r_2009/Library/Caches/pypoetry/virtualenvs/coffeeshop-It7qMoV3-py3.12/lib/python3.12/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2164, in <module>
      register_finder(pkgutil.ImpImporter, find_on_path)
                      ^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?

错误原因

Python 3.12正式移除了pkgutil.ImpImporter,但当前使用的Poetry关联的pip版本过旧,仍在调用已被移除的API,导致报错。

解决步骤

  1. 升级Poetry到最新版本
    更新Poetry可确保它适配Python 3.12的pip版本:
poetry self update
  1. 手动升级虚拟环境内的pip
    若升级Poetry后问题仍存在,直接在Poetry虚拟环境中更新pip:
poetry run pip install --upgrade pip
  1. 重新执行安装命令
    完成上述操作后,再次运行安装:
poetry install
  1. 可选:升级pygame版本
    pygame 2.5.2对Python 3.12的支持可能不完善,可尝试在pyproject.toml中指定更新版本:
pygame = "^2.6.0"

之后再执行poetry install。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:06:05