从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 .
如果您想离线安装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及其依赖项
如果包在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).
离线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
从Pypi下载wheel文件(例如ddb -0.5.0-py3-none-any.whl)
pip install dlb-0.5.0-py3-none-any.whl
推荐文章
- 如何激活蟒蛇环境
- 省略[…]意思是在一个列表里?
- 为什么我得到“'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 -安装特定的软件包版本