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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:41:51  来源:igfitidea点击:

Bash Script to Conda Install requirements.txt with PIP follow-up

pythonbashpipanaconda

提问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 PackageNotFoundErrorthen 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.txtwith 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 condacommand to update your packages in your python environment then I recommend using the environment.ymlfile instead of requirements.txt.

我个人认为 Anaconda 环境和包管理非常出色。因此,如果您使用该conda命令来更新您的 Python 环境中的软件包,那么我建议您使用该environment.yml文件而不是requirements.txt.

The environment.ymlshould 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: rootwhich is the default anaconda environment name. This is not standard practice of how to use the condaand environment.ymlfile. Ideally every python project should have its own environment.ymlfile 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 环境名称。这不是如何使用condaandenvironment.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