从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。
当前回答
从Pypi下载wheel文件(例如ddb -0.5.0-py3-none-any.whl)
pip install dlb-0.5.0-py3-none-any.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 .
下载tarball,将它转移到你的FreeBSD机器并提取它,然后运行python setup.py install,你就完成了!
编辑:在此基础上,您现在还可以使用pip安装焦油球。
如果包在PYPI上,请将它及其依赖项下载到某个本地目录。 如。
$ mkdir /pypi && cd /pypi $ ls -la -rw-r--r-- 1 pavel staff 237954 Apr 19 11:31 Flask-WTF-0.6.tar.gz -rw-r--r-- 1 pavel staff 389741 Feb 22 17:10 Jinja2-2.6.tar.gz -rw-r--r-- 1 pavel staff 70305 Apr 11 00:28 MySQL-python-1.2.3.tar.gz -rw-r--r-- 1 pavel staff 2597214 Apr 10 18:26 SQLAlchemy-0.7.6.tar.gz -rw-r--r-- 1 pavel staff 1108056 Feb 22 17:10 Werkzeug-0.8.2.tar.gz -rw-r--r-- 1 pavel staff 488207 Apr 10 18:26 boto-2.3.0.tar.gz -rw-r--r-- 1 pavel staff 490192 Apr 16 12:00 flask-0.9-dev-2a6c80a.tar.gz
有些包可能必须手动归档到类似的tarball中。当我想要某个东西的最新(不太稳定)版本时,我经常这样做。有些包不在PYPI上,所以同样适用于它们。
假设您在~/src/myapp中有一个格式正确的Python应用程序。~/src/myapp/setup.py将有install_requires列表,其中提到了你在/pypi目录下的一个或多个东西。像这样:
install_requires=[
'boto',
'Flask',
'Werkzeug',
# and so on
如果你想在运行你的应用程序的同时还能运行所有必要的依赖项,你可以这样做:
$ cd ~/src/myapp $ python setup.py develop --always-unzip --allow-hosts=None --find-links=/pypi
这样,你的应用程序将直接从你的源目录执行。你可以侵入一些东西,然后重新运行应用程序而不需要重建任何东西。
如果你想将你的应用程序及其依赖项安装到当前的python环境中,你会这样做:
$ cd ~/src/myapp $ easy_install --always-unzip --allow-hosts=None --find-links=/pypi .
在这两种情况下,如果/pypi目录中没有一个或多个依赖项,构建将失败。它不会试图从互联网上乱安装丢失的东西。
我强烈建议调用setup.py develop…和easy_install…在活动虚拟环境中,以避免污染您的全局Python环境。这是(virtualenv)应该走的路。不要在全局Python环境中安装任何东西。
If the machine that you've built your app has same architecture as the machine on which you want to deploy it, you can simply tarball the entire virtual environment directory into which you easy_install-ed everything. Just before tarballing though, you must make the virtual environment directory relocatable (see --relocatable option). NOTE: the destination machine needs to have the same version of Python installed, and also any C-based dependencies your app may have must be preinstalled there too (e.g. say if you depend on PIL, then libpng, libjpeg, etc must be preinstalled).
使用轮子编译包。
包:
$ 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
对于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
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字