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

在Python虚拟环境安装Ansible 2.9.27时出现zipfile.BadZipFile错误

问题

在Python 3.6.8版本的虚拟环境中安装Ansible 2.9.27包时,执行pip安装出现以下错误:

Looking in indexes: https://pypi.org/simple/
Collecting ansible==2.9.27
  Using cached ansible-2.9.27-py3-none-any.whl
ERROR: Exception:
Traceback (most recent call last):
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/cli/base_command.py", line 164, in exc_logging_wrapper
    status = run_func(*args)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
    return func(self, options, args)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/commands/install.py", line 339, in run
    reqs, check_supported_wheels=not options.target_dir
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/resolver.py", line 93, in resolve
    collected.requirements, max_rounds=try_to_avoid_resolution_too_deep
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_vendor/resolvelib/resolvers.py", line 482, in resolve
    state = resolution.resolve(requirements, max_rounds=max_rounds)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_vendor/resolvelib/resolvers.py", line 349, in resolve
    self._add_to_criteria(self.state.criteria, r, parent=None)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_vendor/resolvelib/resolvers.py", line 173, in _add_to_criteria
    if not criterion.candidates:
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_vendor/resolvelib/structs.py", line 151, in __bool__
    return bool(self._sequence)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 155, in __bool__
    return any(self)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 143, in <genexpr>
    return (c for c in iterator if id(c) not in self._incompatible_ids)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/found_candidates.py", line 47, in _iter_built
    candidate = func()
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/factory.py", line 206, in _make_candidate_from_link
    version=version,
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 287, in __init__
    version=version,
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 156, in __init__
    self.dist = self._prepare()
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 225, in _prepare
    dist = self._prepare_distribution()
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/resolution/resolvelib/candidates.py", line 292, in _prepare_distribution
    return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/operations/prepare.py", line 482, in prepare_linked_requirement
    return self._prepare_linked_requirement(req, parallel_builds)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/operations/prepare.py", line 550, in _prepare_linked_requirement
    self.build_isolation,
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/operations/prepare.py", line 59, in _get_prepared_distribution
    return abstract_dist.get_metadata_distribution()
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/distributions/wheel.py", line 26, in get_metadata_distribution
    return get_wheel_distribution(wheel, canonicalize_name(self.req.name))
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/metadata/__init__.py", line 51, in get_wheel_distribution
    return Distribution.from_wheel(wheel, canonical_name)
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/metadata/pkg_resources.py", line 37, in from_wheel
    with wheel.as_zipfile() as zf:
  File "/opt/dev/automated_golive/development/custom-python3-ansible-299/lib64/python3.6/site-packages/pip/_internal/metadata/base.py", line 321, in as_zipfile
    return zipfile.ZipFile(self.location, allowZip64=True)
  File "/usr/lib64/python3.6/zipfile.py", line 1131, in __init__
    self._RealGetContents()
  File "/usr/lib64/python3.6/zipfile.py", line 1198, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

安装Ansible后续版本可正常进行,已知该Ansible版本和Python版本均已停止支持,询问此问题的原因。

原因分析

  • 缓存的wheel包损坏:从日志里的Using cached ansible-2.9.27-py3-none-any.whl能看出,pip调用了本地缓存的wheel文件,但这个文件已经损坏——wheel本质是zip格式的包,损坏后无法被正常识别解压,所以触发了错误。后续版本安装正常,是因为它们的缓存文件完好,或者你之前没装过这些版本,直接从PyPI下载了完整有效的包。
  • 新旧软件的兼容性冲突:Python 3.6.8和Ansible 2.9.27都已停止维护,而你当前环境的pip可能是较新版本。新版pip对wheel包的格式校验更严格,老旧wheel包的格式可能不符合新版pip的预期,导致被判定为无效zip文件。
  • PyPI上的包文件异常(概率较低):虽然可能性小,但也不排除PyPI上Ansible 2.9.27的wheel包本身存在损坏的情况,不过你能正常安装后续版本,说明PyPI连接正常,这个因素的影响可以忽略。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 22:15:36