从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。
当前回答
使用轮子编译包。
包:
$ 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 .
从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必须替换为所需包的原始名称。
我也遇到过类似的问题。我必须让它以同样的方式安装,我们从pypi。
我做了以下事情:
创建一个目录来存储机器中所有可以访问internet的包。 Mkdir -p /path/to/packages/ 将所有包下载到该路径下
编辑:你也可以试试: Python3 -m PIP wheel——no-cache-dir -r requirements.txt -w /path/to/packages .txt
pip download -r requirements.txt -d /path/to/packages
Eg:- ls /root/wheelhouse/ # **/root/wheelhouse** is my **/path/to/packages/**
total 4524
-rw-r--r--. 1 root root 16667 May 23 2017 incremental-17.5.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 34713 Sep 1 10:21 attrs-18.2.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 3088398 Oct 15 14:41 Twisted-18.9.0.tar.bz2
-rw-r--r--. 1 root root 133356 Jan 28 15:58 chardet-3.0.4-py2.py3-none-any.whl
-rw-r--r--. 1 root root 154154 Jan 28 15:58 certifi-2018.11.29-py2.py3-none-any.whl
-rw-r--r--. 1 root root 57987 Jan 28 15:58 requests-2.21.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 58594 Jan 28 15:58 idna-2.8-py2.py3-none-any.whl
-rw-r--r--. 1 root root 118086 Jan 28 15:59 urllib3-1.24.1-py2.py3-none-any.whl
-rw-r--r--. 1 root root 47229 Jan 28 15:59 tqdm-4.30.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 7922 Jan 28 16:13 constantly-15.1.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 164706 Jan 28 16:14 zope.interface-4.6.0-cp27-cp27mu-manylinux1_x86_64.whl
-rw-r--r--. 1 root root 573841 Jan 28 16:14 setuptools-40.7.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 37638 Jan 28 16:15 Automat-0.7.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 37905 Jan 28 16:15 hyperlink-18.0.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 52311 Jan 28 16:15 PyHamcrest-1.9.0-py2.py3-none-any.whl
-rw-r--r--. 1 root root 10586 Jan 28 16:15 six-1.12.0-py2.py3-none-any.whl
Tar包目录,并将其复制到没有互联网接入的机器。那么做的, / /包/ cd /路径 Tar -cvzf packages.tar.gz。#不是。(点)在结尾
将packages.tar.gz复制到没有互联网接入的目标机器。
在没有互联网接入的机器中,执行以下操作(假设您将tar包复制到当前机器中的/path/to/package/) / /包/ cd /路径 Tar -xvzf packages.tar.gz mkdir -p $HOME/.config/pip/ vi $ HOME / config /脉冲/ pip.conf
然后将下面的内容粘贴进去并保存。
[global]
timeout = 10
find-links = file:///path/to/package/
no-cache-dir = true
no-index = true
最后,我建议您使用某种形式的virtualenv来安装软件包。 Virtualenv -p python2 venv #使用python3,如果你在python3 源。/ venv / bin /激活 PIP install <package>
您应该能够下载目录/路径/到/package/下的所有模块。
注意:我只是这样做,因为我不能添加选项或改变我们安装模块的方式。否则我早就这么做了
pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt
如果您想离线安装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及其依赖项
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录