Python 使用“conda install --yes --file requirements.txt”只安装可用的包,没有错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35802939/
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-08-19 16:58:06  来源:igfitidea点击:

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

pythonpipanaconda

提问by cdeepakroy

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

通过以下命令使用 Conda 在 requirements.txt 中安装软件包时

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:

如果 requirements.txt 中的包不可用,则会抛出“无包错误”,如下所示:

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

使用 Anaconda Cloud api 站点https://api.anaconda.org

获取包元数据:....

错误:在当前的 linux-64 通道中找不到匹配的包:nimfa ==1.2.3

你可以在 anaconda.org 上搜索这个包

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?

除了抛出错误之外,是否可以更改此行为,以便在 requirements.txt 中安装所有可用的包并对不可用的包发出警告?

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.txtto just throw a warning for unavailable packages, I can follow it up with the command pip install -r requirments.txtin .travis.yml so TravisCI attempts to install it from either place where it is available.

我想要这个,因为错误说的包 nimfa 不可用,可以安装 pip。因此,如果我可以将行为更改conda install --yes --file requirements.txt为仅对不可用的包发出警告,我可以使用pip install -r requirments.txt.travis.yml 中的命令进行跟进,以便 TravisCI 尝试从任何可用的地方安装它。

回答by Till Hoffmann

I ended up just iterating over the lines of the file

我最终只是遍历文件的行

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

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

Edit: If you would like to install a package using pip if it is not available through conda, give this a go:

编辑:如果您想使用 pip 安装一个包,如果它不能通过 conda 获得,请试一试:

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

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

Edit: If you are using Windows (credit goes to @Clay):

编辑:如果您使用的是 Windows(归功于@Clay):

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

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

回答by pbms

You can do this as mentioned in this

您可以按照此中所述执行此操作

Export to .yml file

导出到 .yml 文件

conda env export > freeze.yml

To reproduce:

重现:

conda env create -f freeze.yml

回答by Yuri-M-Dias

For those looking, I used this as @TillHoffmann 's solution for the fish shell:

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

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

And

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

回答by MarredCheese

Pbms's answer here is the right way to do it, assuming you have an existing environment to copy off of. Conda is fully capable of installing both Conda packages and pip packages, as listed in environment.yml. I wanted to document the whole process in more detail. Note that I am using folder-based environments, which is why I added --prefix [path to environment folder]to most of the commands.

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

Say you installed an environment for an existing project to a folder called envin the current folder, like this:

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

conda create --prefix ./env

You'd generate environment.ymlfor that project's environment like this:

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

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

You'd create a new environment within some other folder by copying environment.ymlto there and then running this from there:

您可以通过复制environment.yml到其他文件夹然后从那里运行来在其他文件夹中创建一个新环境:

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

You'd get an already-existing environment to match environment.ymlby once again copying environment.ymlto there and then running this from there:

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

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

With the environment in question active, you'd verify the state of its packages like this:

在有问题的环境处于活动状态时,您可以像这样验证其包的状态:

conda list

This is an abridged version of what that command might print (note that the pip packages are marked pypi):

这是该命令可能打印的内容的删节版本(注意 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

Finally, this is an abridged version of what environment.ymlmight look like (note that the pip packages are listed in their own category):

最后,这是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

Be aware that using Conda and pip together can cause some heartburn because they can unknowingly blow away each other's dependencies. You are supposed to install all of your Conda packages first and then all of your pip packages afterward, rather than alternating between the two. If your environment breaks, the official recommendation is to delete and recreate it (from your environment.ymlfile). For more details, see this guide:

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

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

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