Install only available packages using "conda install --yes --file requirements.txt" without error

时间:2016-03-04 17:58:22

标签: python anaconda

While installing packages in requirements.txt using conda through the following command

conda install --yes --file requirements.txt

If a package in requirements.txt is not available then it throws a "No package error" such as the one shown below:

Using Anaconda Cloud api site https://api.anaconda.org

Fetching package metadata: ....

Error: No packages found in current linux-64 channels matching: nimfa ==1.2.3

You can search for this package on anaconda.org with

anaconda search -t conda nimfa ==1.2.3

Instead of throwing an error, is it possible to change this behavior such that it installs all the available packages in requirements.txt and throws a warning for those that are not available?

I would like this because, the package nimfa which the error says is not available, can be pip installed. So if i can change the behavior of conda install --yes --file requirements.txt to just throw a warning for unavailable packages, i can follow it up with the command pip install -r requirments.txt in .travis.yml so TravisCI attempts to install it from either place where it is available.

4 个答案:

答案 0 :(得分:110)

我最后只是迭代了文件的行

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

编辑:如果你想使用pip安装一个软件包,如果它不能通过conda获得,那么就去吧:

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

编辑:如果您使用的是Windows(信用转到@Clay):

$ FOR /F "delims=~" %f in (requirements.txt) DO conda install --yes "%f" || pip install "%f"

答案 1 :(得分:5)

您可以按照this

中的说明执行此操作

导出到.yml文件

conda env export > freeze.yml

重现:

conda env create -f freeze.yml

答案 2 :(得分:1)

对于那些寻找的人,我将其用作@TillHoffmann的鱼壳解决方案:

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

还有

$ while read requirement; conda install --yes $requirement;or pip install $requirement; end < requirements.txt

答案 3 :(得分:0)

Pbms的答案是正确的方法,前提是您要复制现有的环境。如environment.yml中所列,Conda完全能够安装Conda软件包和pip软件包。我想更详细地记录整个过程。请注意,我正在使用基于文件夹的环境,这就是为什么我在大多数命令中添加了--prefix [path to environment folder]的原因。

假设您将现有项目的环境安装到当前文件夹中名为env的文件夹中,如下所示:

conda create --prefix ./env

您将为该项目的环境生成environment.yml,如下所示:

conda env export --prefix ./env > environment.yml

您可以在其他文件夹中创建一个新环境,方法是将environment.yml复制到那里,然后从那里运行它:

conda env create --prefix ./env --file environment.yml

通过将environment.yml再次复制到那里,然后从那里运行,可以得到一个已经存在的环境来匹配environment.yml

conda env update --prefix ./env --file environment.yml --prune

在有问题的环境处于活动状态的情况下,您将像这样验证其软件包的状态:

conda list

这是该命令可能显示的缩写版本(请注意,pip包已标记为pypi):

# Name                    Version                   Build  Channel
pip                       19.2.2                   py37_0
python                    3.7.4                h5263a28_0
numpy                     1.16.4           py37h19fb1c0_0
pandas                    0.25.1           py37ha925a31_0
pyodbc                    4.0.27           py37ha925a31_0
ibm-db                    3.0.1                    pypi_0    pypi
ibm-db-sa                 0.3.5                    pypi_0    pypi

最后,这是environment.yml的简化版本(请注意,pip包在其自己的类别中列出):

dependencies:
  - pip=19.2.2=py37_0
  - python=3.7.4=h5263a28_0
  - numpy=1.16.4=py37h19fb1c0_0
  - pandas=0.25.1=py37ha925a31_0
  - pyodbc=4.0.27=py37ha925a31_0
  - pip:
    - ibm-db==3.0.1
    - ibm-db-sa==0.3.5

请注意,同时使用Conda和pip可能会引起胃灼热,因为它们会在不知不觉中破坏彼此的依赖性。您应该先安装所有的Conda软件包,然后再安装所有的pip软件包,而不要在这两者之间交替进行。如果您的环境损坏,则官方建议删除并重新创建(从您的environment.yml文件中)。有关更多详细信息,请参见本指南:

https://www.anaconda.com/using-pip-in-a-conda-environment/