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


当前回答

对于Windows,我使用了下面的东西

网络连接

1.创建任意名称的目录。用"repo"创建的

2.使用以下命令下载库(将下载而不是安装)

pip下载libraray_name -d"C:\repo"

pip download openpyxl -d"C:\repo"

然后您将发现多个.whl扩展名文件 复制requirements.txt中的所有文件名

没有互联网连接

现在将此文件夹和文件移动到没有互联网连接的PC并运行以下命令。

pip install -r requirements.txt --find-links=C:\repo --no-index

其他回答

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

在可以访问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

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

对于Windows,我使用了下面的东西

网络连接

1.创建任意名称的目录。用"repo"创建的

2.使用以下命令下载库(将下载而不是安装)

pip下载libraray_name -d"C:\repo"

pip download openpyxl -d"C:\repo"

然后您将发现多个.whl扩展名文件 复制requirements.txt中的所有文件名

没有互联网连接

现在将此文件夹和文件移动到没有互联网连接的PC并运行以下命令。

pip install -r requirements.txt --find-links=C:\repo --no-index

使用轮子编译包。

包:

$ 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

作为一个继续@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 .