Github Actions中Setuptools通过Poetry随机安装失败求助
Github Actions中Poetry安装setuptools随机失败的解决思路
问题描述
近几周Github Actions工作流在安装setuptools库时随机失败,通常重新运行1-2次即可解决,但希望从根源避免该问题。
环境信息
- Github Actions运行环境:ubuntu-latest
- Python版本:3.10.13(已测试3.11.6和3.11.8)
- setuptools版本:pyproject.toml中指定为69.1.1,但Poetry会自动更新至69.2.0
- 安装命令:
poetry config virtualenvs.create false && poetry install --no-root
错误日志
... - Updating setuptools (65.5.0 -> 69.1.1) ChefInstallError Failed to install setuptools >= 40.8.0. Output: Updating dependencies Resolving dependencies... Package operations: 1 install, 0 updates, 0 removals - Installing setuptools (69.2.0) CalledProcessError Command '['/tmp/tmpd2m4qzu7/.venv/bin/python', '-I', '-W', 'ignore', '-c', '\nimport importlib.util\nimport json\nimport sys\n\nfrom pathlib import Path\n\nspec = importlib.util.spec_from_file_location(\n "packaging", Path(r"/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/__init__.py")\n)\npackaging = importlib.util.module_from_spec(spec)\nsys.modules[spec.name] = packaging\n\nspec = importlib.util.spec_from_file_location(\n "packaging.tags", Path(r"/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/tags.py")\n)\npackaging_tags = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(packaging_tags)\n\nprint(\n json.dumps([(t.interpreter, t.abi, t.platform) for t in packaging_tags.sys_tags()])\n)\n']' returned non-zero exit status 1. at /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/subprocess.py:526 in run 522│ # We don't call process.wait() as .__exit__ does that for us. 523│ raise 524│ retcode = process.poll() 525│ if check and retcode: → 526│ raise CalledProcessError(retcode, process.args, 527│ output=stdout, stderr=stderr) 528│ return CompletedProcess(process.args, retcode, stdout, stderr) 529│ 530│ The following error occurred when trying to handle this error: EnvCommandError Command ['/tmp/tmpd2m4qzu7/.venv/bin/python', '-I', '-W', 'ignore', '-c', '\nimport importlib.util\nimport json\nimport sys\n\nfrom pathlib import Path\n\nspec = importlib.util.spec_from_file_location(\n "packaging", Path(r"/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/__init__.py")\n)\npackaging = importlib.util.module_from_spec(spec)\nsys.modules[spec.name] = packaging\n\nspec = importlib.util.spec_from_file_location(\n "packaging.tags", Path(r"/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/tags.py")\n)\npackaging_tags = importlib.util.module_from_spec(spec)\nspec.loader.exec_module(packaging_tags)\n\nprint(\n json.dumps([(t.interpreter, t.abi, t.platform) for t in packaging_tags.sys_tags()])\n)\n'] errored with the following return code 1 Error output: Traceback (most recent call last): File "<string>", line 18, in <module> File "<frozen importlib._bootstrap_external>", line 883, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/tags.py", line 26, in <module> File "/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/_manylinux.py", line 10, in <module> File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 879, in exec_module File "<frozen importlib._bootstrap_external>", line 1016, in get_code File "<frozen importlib._bootstrap_external>", line 1073, in get_data FileNotFoundError: [Errno 2] No such file or directory: '/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/packaging/_elffile.py' at /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/poetry/utils/env/base_env.py:342 in _run 338│ output = subprocess.check_output( 339│ cmd, stderr=stderr, env=env, text=True, **kwargs 340│ ) 341│ except CalledProcessError as e: → 342│ raise EnvCommandError(e) 343│ 344│ return output 345│ 346│ def execute(self, bin: str, *args: str, **kwargs: Any) -> int: Cannot install setuptools. Error: at /opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/poetry/installation/chef.py:102 in install 98│ InstalledRepository.load(self._env), 99│ ) 100│ installer.update(True) 101│ if installer.run() != 0: → 102│ raise ChefInstallError(requirements, io.fetch_output(), io.fetch_error()) 103│ 104│ 105│ class Chef: 106│ def __init__( Cannot install build-system.requires for odfpy.
已尝试方案
- 更换多个Python版本,问题依旧
- 在安装Poetry前用pip预安装setuptools,无效
解决思路
清理pip缓存:随机失败大概率是缓存的损坏依赖包导致,在安装步骤前添加缓存清理命令:
pip cache purge固定packaging库版本:错误核心是
packaging/_elffile.py缺失,说明packaging库安装不完整或版本兼容问题,可提前安装固定版本:pip install packaging==23.2也可以在pyproject.toml中添加依赖锁定:
[tool.poetry.dependencies] packaging = "23.2"改用虚拟环境安装:当前使用全局环境(
virtualenvs.create false)容易引发依赖冲突,建议移除该配置,让Poetry默认创建独立虚拟环境:poetry install --no-root锁定所有依赖版本:生成并提交
poetry.lock文件到仓库,确保每次安装的依赖版本完全一致,避免Poetry自动更新依赖:poetry lock --no-update更新Poetry到最新版本:可能是Poetry自身的安装逻辑bug,更新到最新版尝试修复:
curl -sSL https://install.python-poetry.org | python3 - --version latest
内容的提问来源于stack exchange,提问作者Laura Montes
相关产品推荐
相关产品推荐

