离线安装Python包

起因

需要在一台联网的计算机和一台不联网的计算机上安装 Python 包。

pygame 在线安装失败

在 CMD 命令行运行下列命令安装 pygame:
python -m pip install --user pygamepip install pygame

超时安装失败!

离线安装 pygame

通过博文找到离线下载地址:https://pypi.org/project/pygame/#files

下载后通过以下命令安装:pip install pygame-***-win_amd64.whl

但尝试了几个以 win_amd64.whl 结尾的包均以失败告终。

通过博文了解到可通过 pip debug --verbose 命令查看 .whl 包的规范命名。

我安装的是 Python3.11,使用的是 win64 位系统,查看到的规范命名是:

Compatible tags: 39
  cp311-cp311-win_amd64

相关规范名称的包在 pypi.org 上找不到!

后来在 GitHub 上找到了下载地址:https://github.com/pygame/pygame/releases

原来支持 Python3.11 的 pygame 上属于预览版:
https://github.com/pygame/pygame/releases/download/2.1.3.dev8/pygame-2.1.3.dev8-cp311-cp311-win_amd64.whl

使用以下命令离线安装成功:pip install pygame-2.1.3.dev8-cp311-cp311-win_amd64.whl

离线安装 matplotlib

然而在matplotlib官网和GitHub并未找到离线安装包,但找到一个非官方下载的站点:https://www.lfd.uci.edu/~gohlke/pythonlibs/

可以下载到兼容 Python3.11 的包,但不是最新版的 matplotlib。

安装 plotly

plotly官网、Github、及上述离线下载站点均未找到离线安装包。

使用 pip --help 查看帮助,可使用 timeout 避免下载超时失败:

pip install plotly==5.11.0 --timeout 100

pip --help

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  inspect                     Inspect the python environment.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  config                      Manage local and global configuration.
  search                      Search PyPI for packages.
  cache                       Inspect and manage pip's wheel cache.
  index                       Inspect information available from package indexes.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  debug                       Show information useful for debugging.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --debug                     Let unhandled exceptions propagate outside the main subroutine, instead of logging them
                              to stderr.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  --require-virtualenv        Allow pip to only run in a virtual environment; exit with an error otherwise.
  --python <python>           Run pip with the specified Python interpreter.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to
                              WARNING, ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form scheme://[user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup,
                              (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any
                              HTTPS.
  --cert <path>               Path to PEM-encoded CA certificate bundle. If provided, overrides the default. See 'SSL
                              Certificate Verification' in pip documentation for more information.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the
                              certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for
                              download. Implied with --no-index.
  --no-color                  Suppress colored output.
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be removed in the future.

在线安装 plotly 过程中,使用 Everything 搜索,plotly*.whl,查找到 Temp 文件夹下存在名称为 plotly-5.11.0-py2.py3-none-any.whl 的文件,在下载完成后安装前复制该文件(安装完成后该文件将自动删除,必须赶在安装时,自动删除前复制,这个很短,不太好操作)即可得到离线安装包。

2023-9-23 补充

虽然可以通过上面的延长 timeout 时间解决一些下载失败问题,但有些包可能下载时间太长;且一个包可能依赖很多其他的包;其下载后包会自动删除,不便复制。下面解决这些问题。

查看安装的包
pip list

卸载包
pip uninstall scipy

使用清华镜像安装包
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy

使用清华镜像下载包
pip download scipy -d d:/packages/ -i https://pypi.tuna.tsinghua.edu.cn/simple

提示下载成功:
...
Saved d:\packages\scipy-1.11.2-cp311-cp311-win_amd64.whl
Saved d:\packages\numpy-1.26.0-cp311-cp311-win_amd64.whl
...

将包及其依赖项保存为(顺序与下载时显示的相反):requirements.txt

d:\packages\numpy-1.26.0-cp311-cp311-win_amd64.whl
d:\packages\scipy-1.11.2-cp311-cp311-win_amd64.whl

安装包及其依赖:
pip install -r d:/packages/requirements.txt

批量将本机全部安装包转移到另外一台电脑安装方法

若要在两台电脑上同步大量的包,下面的方法更简便:

Output installed packages in requirements format. 获取本机全部安装包
pip freeze > requirements.txt

下载全部
pip download -r requirements.txt -d d:/packages/ --timeout 600

太慢还是用清华镜像
pip download -r requirements.txt -d d:/packages/ -i https://pypi.tuna.tsinghua.edu.cn/simple

posted @ 2023-01-08 17:41  X2009  阅读(1323)  评论(0编辑  收藏  举报