从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。
当前回答
在可以访问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
在不访问网络的情况下安装那些下载的模块。
其他回答
从Pypi下载wheel文件(例如ddb -0.5.0-py3-none-any.whl)
pip install dlb-0.5.0-py3-none-any.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
在不访问网络的情况下安装那些下载的模块。
作为一个继续@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。
我做了以下事情:
创建一个目录来存储机器中所有可以访问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
对于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
推荐文章
- 如何激活蟒蛇环境
- 省略[…]意思是在一个列表里?
- 为什么我得到“'str'对象没有属性'读取'”当尝试使用' json。载入字符串?
- 不区分大小写的列表排序,没有降低结果?
- 排序后的语法(key=lambda:…)
- 在烧瓶中返回HTTP状态代码201
- 使用python创建一个简单的XML文件
- APT命令行界面式的yes/no输入?
- 如何打印出状态栏和百分比?
- 在Python中获取大文件的MD5哈希值
- 在Python格式字符串中%s是什么意思?
- 如何循环通过所有但最后一项的列表?
- python用什么方法避免默认参数为空列表?
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本