pip离线安装Python包

以离线安装mypy包为例

准备工作

需要确保在线和离线机器的 Python 版本、操作系统 (OS)、CPU 架构 完全一致。

基础流程:下载单个包

1. 在线下载

在有网机器上,pip download 会下载包及其所有依赖,但不安装。包名可以指定版本,如mypy==1.19.1,也可以指定版本范围,如mypy>1.19,<1.20

python -m pip download mypy -d Test\mypy

输出示例:

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting mypy
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0e/fd/3e82603a0cb66b67c5e7abababce6bf1a929ddf67bf445e652684af5c5a0/mypy-1.19.1-cp310-cp310-win_amd64.whl (10.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1/10.1 MB 2.0 MB/s  0:00:04
Collecting typing_extensions>=4.6.0 (from mypy)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl (44 kB)
Collecting mypy_extensions>=1.0.0 (from mypy)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/79/7b/2c79738432f5c924bef5071f933bcc9efd0473bac3b4aa584a6f7c1c8df8/mypy_extensions-1.1.0-py3-none-any.whl (5.0 kB)
Collecting pathspec>=0.9.0 (from mypy)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/32/2b/121e912bd60eebd623f873fd090de0e84f322972ab25a7f9044c056804ed/pathspec-1.0.3-py3-none-any.whl (55 kB)
Collecting tomli>=1.1.0 (from mypy)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/23/d1/136eb2cb77520a31e1f64cbae9d33ec6df0d78bdf4160398e86eec8a8754/tomli-2.4.0-py3-none-any.whl (14 kB)
Collecting librt>=0.6.2 (from mypy)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fe/91/c4202779366bc19f871b4ad25db10fcfa1e313c7893feb942f32668e8597/librt-0.7.8-cp310-cp310-win_amd64.whl (49 kB)
Saved d:\python310\test\mypy\mypy-1.19.1-cp310-cp310-win_amd64.whl
Saved d:\python310\test\mypy\librt-0.7.8-cp310-cp310-win_amd64.whl
Saved d:\python310\test\mypy\mypy_extensions-1.1.0-py3-none-any.whl
Saved d:\python310\test\mypy\pathspec-1.0.3-py3-none-any.whl
Saved d:\python310\test\mypy\tomli-2.4.0-py3-none-any.whl
Saved d:\python310\test\mypy\typing_extensions-4.15.0-py3-none-any.whl
Successfully downloaded mypy librt mypy_extensions pathspec tomli typing_extensions

pip download还可以可以指定下面参数:

  • --python-version 指定python版本,如:38
  • --platform 指定运行的系统平台,如:win_amd64manylinux2014_x86_64manylinux2014_aarch64macosx_11_0_arm64macosx_10_15_x86_64musllinux_1_2_x86_64
  • --abi二进制接口版本,如:cp310cp311cp3121cp39cp314cp38
  • --no-deps 不下载依赖的包
  • --prefer-binary优先选择二进制包而不是源包,即使源包较新。

更多参数请运行python -m pip download --help来查看。

2. 拷贝并安装

mypy 目录拷贝到离线机器,执行安装。--no-index 禁止访问网络,--find-links 指定本地包位置。

python -m pip install --no-index --find-links=Test\mypy mypy

输出示例:

Looking in links: Test
Processing d:\python310\test\mypy-1.19.1-cp310-cp310-win_amd64.whl
Requirement already satisfied: typing_extensions>=4.6.0 in d:\python310\lib\site-packages (from mypy) (4.15.0)
Requirement already satisfied: mypy_extensions>=1.0.0 in d:\python310\lib\site-packages (from mypy) (1.1.0)
Processing d:\python310\test\pathspec-1.0.3-py3-none-any.whl (from mypy)
Requirement already satisfied: tomli>=1.1.0 in d:\lidar360_dev72\bin\python310\lib\site-packages (from mypy) (2.3.0)
Processing d:\python310\test\librt-0.7.8-cp310-cp310-win_amd64.whl (from mypy)
Installing collected packages: pathspec, librt, mypy
   ━━━━━━━━━━━━━━━━━━━━━━━━━━╸━━━━━━━━━━━━━ 2/3 [mypy]  WARNING: The scripts dmypy.exe, mypy.exe, mypyc.exe, stubgen.exe and stubtest.exe are installed in 'D:\python310\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed librt-0.7.8 mypy-1.19.1 pathspec-1.0.3

批量包下载安装方式

1. 批量处理 (requirements.txt)

将所有依赖写入 requirements.txt,实现一键打包和安装。

  • 在线下载所有依赖:

    python -m pip download -r requirements.txt -d packages
    
  • 离线安装:

    python -m pip install --no-index --find-links=packages -r requirements.txt
    

2. 避免离线编译 (pip wheel)

pip download 可能下载源码包,导致离线安装时因缺少编译器而失败。pip wheel 会提前将源码编译成本地平台的 .whl 文件,更可靠。

  • 在线预编译并下载:

    python -m pip wheel -r requirements.txt -w wheelhouse
    
  • 离线安装 (命令与 download 方式相同):

    python -m pip install --no-index --find-links=wheelhouse -r requirements.txt
    

3. 跨平台下载

如果目标平台与当前平台不同(例如从 Windows 机器为 Linux 机器打包),可尝试指定目标平台,但前提是 PyPI 上存在该平台的预编译包。

  • 为 Linux x86_64 打包 (Python 3.10):

    python -m pip download mypy --platform manylinux2014_x86_64 --python-version 310 --only-binary=:all: -d packages
    

    注意:此方法并非对所有包都有效。若无对应平台的 wheel 包,仍需在目标平台在线构建。

posted @ 2026-01-27 13:56  乌合之众  阅读(18)  评论(0)    收藏  举报
clear