Bash脚本到Conda使用PIP后续安装requirements.txt

问题描述 投票:5回答:3

在linux服务器上安装Django应用程序的requirements.txt文件时,我可以运行:

conda install --yes --file requirements.txt

如果通过Conda(PackageNotFoundError)无法获得任何软件包,则会崩溃。这个bash one liner是一个很酷的方式来一次通过requirements.txt文件一行source

while read requirement; do conda install --yes $requirement; done < requirements.txt

这将安装通过Conda可用的所有软件包,而不会崩溃第一个丢失的软件包。但是,我想通过捕获Conda的输出来跟踪失败的软件包,如果有PackageNotFoundError,则在软件包上运行pip install。

我对bash不太满意,所以希望有人可以推荐一个hack。另一种解决方案可能是写出一个名为pip-requirements.txt的新文本文件,其中包含失败的要求。

python bash pip anaconda
3个回答
3
投票

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

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

要安装的命令:

conda env update --file environment.yml

Note:

在这里我们设置name: root这是默认的anaconda环境名称。这不是如何使用condaenvironment.yml文件的标准做法。理想情况下,每个python项目都应该有自己的environment.yml文件,该文件具有项目特定的环境名称,即name: project-name。请通过https://conda.io/docs/user-guide/tasks/manage-environments.html使用Anaconda进行包管理。


1
投票

将stderr重定向到文件:

while read requirement; do conda install --yes $requirement; done < requirements.txt 2>error.log

1
投票

找到了解决方案:

如果包不可用于conda,请运行此命令以使用conda或pip进行安装:

while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt 

完成后,您可以使用conda导出环境yaml:

conda env export > environment.yml
© www.soinside.com 2019 - 2024. All rights reserved.