根本原因是 pip 默认联网查询 pypi,必须用 --no-index 禁用索引并配合 --find-links file:///path/to/wheels 指向本地 wheel 目录,且 wheel 文件平台标签须与目标机完全一致。

离线安装时 pip install 报错 ERROR: Could not find a version that satisfies the requirement
根本原因是 pip 默认会联网查询 PyPI,即使你指定了本地 .whl 文件,它仍可能尝试解析依赖并远程校验。必须彻底禁用网络行为。
- 加
--find-links file:///path/to/wheels指向本地 wheel 目录(注意是file://协议,不是相对路径) - 必须同时加
--no-index,否则 pip 仍会 fallback 到 PyPI - 如果 wheel 有嵌套依赖(比如
numpy-1.24.3-cp39-cp39-win_amd64.whl依赖openblas),要确保所有依赖 wheel 都在该目录下——pip 不会递归扫描子目录 - 推荐用
pip install --find-links file:///D:/wheels --no-index -r requirements.txt,其中requirements.txt写的是包名+版本(如requests==2.31.0),不是 wheel 文件名
如何生成兼容目标环境的 wheel 文件(尤其是 Windows + 特定 Python 版本)
在内网部署机上直接 pip wheel 最稳妥;若只能在外网打包,必须严格匹配:Python 版本、架构(win_amd64 / win32)、是否启用 UCS-2/UTF-8(影响 cp39 等标签)。
- 查目标机信息:
python -c "import platform; print(platform.architecture(), platform.machine(), platform.python_version())" - 外网打包时加
--platform win_amd64 --python-version 39 --abi cp39 --only-binary=:all:,避免混入源码包(.tar.gz) - 注意 C 扩展包(如
pandas、pyyaml)通常没有纯 Python wheel,必须用对应平台编译好的二进制 wheel,不能靠--no-deps躲避 - 验证 wheel 兼容性:
python -c "from wheel import wheelfile; print(wheelfile.WheelFile('xxx.whl').tags)"
批量安装时依赖冲突或版本不匹配导致失败
pip install 在离线模式下不会自动降级或升級已装包,冲突会直接中断,不像联网时可尝试回溯版本。
- 先清空目标环境:
pip uninstall -y -r requirements.txt(如果已部分安装) - 用
pip install --force-reinstall --no-deps先装所有 wheel,再用pip install --no-index --find-links ... -r requirements.txt补依赖关系(此时 pip 只从本地找,不校验版本冲突) - 更可靠的做法:用
pip wheel --no-deps -w wheels/ -r requirements.txt在外网一次性下载全量 wheel(含依赖),再复制进内网——但要求外网环境与内网完全一致 - 遇到
ImportError: DLL load failed,大概率是 wheel 编译时链接了 VS 运行库(如vcruntime140.dll),需同步部署Microsoft Visual C++ Redistributable
如何验证 wheel 是否真正离线可用
最简单的验证方式,是在无网络的机器上新建干净虚拟环境,执行安装命令并观察是否报任何 ConnectionRefusedError 或 Timeout。
- 启动前断网(拔网线 or
ipconfig /releaseon Windows),不要只关浏览器 - 运行
pip install --find-links file:///X:/wheels --no-index requests,成功则输出Successfully installed requests-...,且过程无 DNS 查询日志(可用 Process Monitor 监控pip.exe的网络请求) - 检查
pip debug --verbose中的index-url是否为默认值(https://pypi.org/simple)——如果是,说明--no-index没生效 - 常见漏点:某些包(如
setuptools)会在安装过程中调用pkg_resources触发隐式联网,建议提前升级到最新版pip和setuptools(用 wheel 安装)
--no-index 和 --find-links 必须成对出现,缺一不可;其次,wheel 文件名中的平台标签(如 cp39、win_amd64)必须和目标机完全一致,差一个字符就会被 pip 当作不兼容而跳过。Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!











