Bash 脚本到 Conda 安装 requirements.txt 并带有 PIP 跟进
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47999477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash Script to Conda Install requirements.txt with PIP follow-up
提问by it's-yer-boy-chet
When installing a requirements.txt file for a Django app on a linux server I can run:
在 linux 服务器上为 Django 应用程序安装 requirements.txt 文件时,我可以运行:
conda install --yes --file requirements.txt
This will crash if any of the packages are not available through Conda (PackageNotFoundError
). This bash one liner is a cool way to go through the requirements.txt file one line at a time source:
如果任何软件包无法通过 Conda ( PackageNotFoundError
)获得,这将崩溃。这个 bash one liner 是一种很酷的方式,可以一次一行地浏览 requirements.txt 文件源:
while read requirement; do conda install --yes $requirement; done < requirements.txt
This installs all of the packages that are available through Conda without crashing on the first missing package. However, what I would like to is track the packages that fail by capturing the output from Conda and if there is a PackageNotFoundError
then run pip install on the package.
这将安装通过 Conda 可用的所有软件包,而不会在第一个丢失的软件包上崩溃。但是,我想要的是通过捕获 Conda 的输出来跟踪失败的包,如果有PackageNotFoundError
然后在包上运行 pip install。
I'm not great with bash, so hoping someone can suggest a hack. Another solution could be to just write out a new text file called pip-requirements.txt
with the requirements that fail.
我不擅长 bash,所以希望有人可以建议一个 hack。另一种解决方案可能是写出一个新的文本文件pip-requirements.txt
,其中包含失败的需求。
采纳答案by it's-yer-boy-chet
Found a solution:
找到了解决办法:
Run this to install with conda or pip if the package is not available to conda:
如果该软件包不可用于 conda,请运行此命令以使用 conda 或 pip 进行安装:
while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
Once it's done you can use conda to export the environment yaml:
完成后,您可以使用 conda 导出环境 yaml:
conda env export > environment.yml
回答by nitred
Personally I find the Anaconda environment & package management to be outstanding. So if you're using the conda
command to update your packages in your python environment then I recommend using the environment.yml
file instead of requirements.txt
.
我个人认为 Anaconda 环境和包管理非常出色。因此,如果您使用该conda
命令来更新您的 Python 环境中的软件包,那么我建议您使用该environment.yml
文件而不是requirements.txt
.
The environment.yml
should looks like the following:
本environment.yml
应该如下所示:
name: root # default is root
channels:
- defaults
dependencies: # everything under this, installed by conda
- numpy==1.13.3
- scipy==1.0.0
- pip: # everything under this, installed by pip
- Flask==0.12.2
- gunicorn==19.7.1
The command to install:
安装命令:
conda env update --file environment.yml
Note:
笔记:
Here we set name: root
which is the default anaconda environment name. This is not standard practice of how to use the conda
and environment.yml
file. Ideally every python project should have its own environment.yml
file with a project specific environment name i.e. name: project-name
. Please go through https://conda.io/docs/user-guide/tasks/manage-environments.htmlon using Anaconda for package management.
这里我们设置name: root
了默认的 anaconda 环境名称。这不是如何使用conda
andenvironment.yml
文件的标准做法。理想情况下,每个 python 项目都应该有自己的environment.yml
文件,其中包含项目特定的环境名称,即name: project-name
. 请通过https://conda.io/docs/user-guide/tasks/manage-environments.html了解使用 Anaconda 进行包管理。
回答by phd
Redirect stderr to a file:
将 stderr 重定向到文件:
while read requirement; do conda install --yes $requirement; done < requirements.txt 2>error.log