从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。


当前回答

作为一个继续@chaokunyang的回答,我想把我写的脚本放在这里:

编写一个“requirements.txt”文件,指定想要打包的库。 创建一个包含所有库的tar文件(参见Packer脚本)。 将创建的tar文件放到目标计算机中并解压缩它。 运行安装程序脚本(它也被打包到tar文件中)。

“让”文件

docker==4.4.0

包装端:文件名:"create-offline-python3.6-dependencies-repository.sh"

#!/usr/bin/env bash

# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983

LIBRARIES_DIR="python3.6-wheelhouse"

if [ -d ${LIBRARIES_DIR} ]; then
    rm -rf ${LIBRARIES_DIR}/*
else
    mkdir ${LIBRARIES_DIR}
fi

pip download -r requirements.txt -d ${LIBRARIES_DIR}

files_to_add=("requirements.txt" "install-python-libraries-offline.sh")

for file in "${files_to_add[@]}"; do
    echo "Adding file ${file}"
    cp "$file" ${LIBRARIES_DIR}
done

tar -cf ${LIBRARIES_DIR}.tar ${LIBRARIES_DIR}

安装端:文件名:"install-python-libraries-offline.sh"

#!/usr/bin/env bash

# This script follows the steps described in this link:
# https://stackoverflow.com/a/51646354/8808983

# This file should run during the installation process from inside the libraries directory, after it was untared:
# pythonX-wheelhouse.tar -> untar -> pythonX-wheelhouse
# |
# |--requirements.txt
# |--install-python-libraries-offline.sh


pip3 install -r requirements.txt --no-index --find-links .

其他回答

从Pypi下载wheel文件(例如ddb -0.5.0-py3-none-any.whl)

pip install dlb-0.5.0-py3-none-any.whl

让我一步一步地介绍一下这个过程:

在连接到互联网的计算机上创建一个文件夹。

   $ mkdir packages
   $ cd packages

打开命令提示符或shell并执行以下命令: 假设你想要的包是tensorflow $ PIP下载tensorflow 现在,在目标计算机上复制packages文件夹并应用以下命令

  $ cd packages
  $ pip install 'tensorflow-xyz.whl' --no-index --find-links '.'

注意tensorflow-xyz。WHL必须替换为所需包的原始名称。

在可以访问internet的系统上

pip download命令允许你下载软件包而不安装它们:

pip download -r requirements.txt

(在pip的以前版本中,这被拼写为pip install——download -r requirements.txt。)

在无法访问internet的系统上

然后你可以使用

pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt

在不访问网络的情况下安装那些下载的模块。

使用轮子编译包。

包:

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ pip wheel -r requirements.txt --wheel-dir=$tempdir
$ cwd=`pwd`
$ (cd "$tempdir"; tar -cjvf "$cwd/bundled.tar.bz2" *)

复制tarball并安装:

$ tempdir=$(mktemp -d /tmp/wheelhouse-XXXXX)
$ (cd $tempdir; tar -xvf /path/to/bundled.tar.bz2)
$ pip install --force-reinstall --ignore-installed --upgrade --no-index --no-deps $tempdir/*

注意轮二进制包不是跨机器的。

更多参考资料请访问:https://pip.pypa.io/en/stable/user_guide/#installation-bundles

对于Pip 8.1.2,您可以使用Pip download -r request .txt将包下载到您的本地机器。