从pypi下载python包及其依赖项以离线安装到另一台机器的最佳方法是什么?使用pip或easy_install是否有简单的方法来做到这一点?我试图在一个没有连接到互联网的FreeBSD盒子上安装请求库。
当前回答
对于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
在不访问网络的情况下安装那些下载的模块。
离线python。为此,我使用virtualenv(孤立的Python环境)
1)安装virtualenv 与pip在线:
pip install virtualenv --user
或离线使用whl:转到此链接,下载最新版本(。WHL或tar.gz),并使用以下命令安装:
pip install virtualenv-15.1.0-py2.py3-none-any.whl --user
通过使用——user,您不需要使用sudo pip....
2)使用virtualenv
在在线机器上选择一个终端CD目录并运行以下代码:
python -m virtualenv myenv
cd myenv
source bin/activate
pip install Flask
安装完所有的包后,你必须生成一个requirements.txt文件,这样当你的virtualenv处于活动状态时,就可以写了
pip freeze > requirements.txt
打开一个新终端并创建另一个env,如myenv2。
python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
ls
现在您可以转到您的脱机文件夹,其中有requirements.txt和transferred_packages文件夹。下载带有以下代码的包,并将它们全部放到transferred_packages文件夹中。
pip download -r requirements.txt
把您的脱机文件夹到脱机计算机,然后
python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
cd offline
pip install --no-index --find-links="./tranferred_packages" -r requirements.txt
离线文件夹[requirements.txt, transferred_packages {Flask-0.10.1.tar.gz,…}]
检查你的包裹清单
pip list
注意:因为我们是在2017年,所以最好使用python 3。使用该命令可以创建python 3 virtualenv。
virtualenv -p python3 envname
作为一个继续@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 .
对于Pip 8.1.2,您可以使用Pip download -r request .txt将包下载到您的本地机器。
如果包在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).
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字