从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 .

其他回答

如果您想离线安装python库及其依赖项,请在具有相同操作系统、网络连接并安装python的机器上完成以下步骤:

1)创建一个包含类似内容的requirements.txt文件(注意-这些是你希望下载的库):

Flask==0.12
requests>=2.7.0
scikit-learn==0.19.1
numpy==1.14.3
pandas==0.22.0

创建需求文件的一个选项是使用pip freeze > requirements.txt。这将列出环境中的所有库。然后您可以进入requirements.txt,删除不需要的文件。

2)执行命令mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse,将lib及其依赖项下载到目录wheelhouse

3)拷贝requirements.txt到wheelhouse目录

4)用tar -zcf wheelhouse.tar.gz归档wheelhouse到wheelhouse.tar.gz

然后上传wheelhouse.tar.gz到你的目标机器:

1)执行tar -zxf wheelhouse.tar.gz解压文件

2)执行pip install -r wheelhouse/requirements.txt——no-index——find-links wheelhouse安装lib及其依赖项

对于Pip 8.1.2,您可以使用Pip download -r request .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

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

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

   $ mkdir packages
   $ cd packages

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

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

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

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