简体   繁体   English

如何确定 conda 是否已经在 bash 脚本中的机器上可用?

[英]How to find out if conda is already available on a machine within a bash script?

I want to create a bash script to install a new virtual environment "ABC" in conda.我想创建一个 bash 脚本来在 conda 中安装一个新的虚拟环境“ABC”。 But before I go ahead and run a command to create this env, I want to check if conda is already installed on the machine.但是在我继续运行命令来创建这个环境之前,我想检查一下机器上是否已经安装了 conda。 If not installed, I want to install miniconda and then create the env "ABC".如果没有安装,我想安装 miniconda 然后创建环境“ABC”。 If conda is already installed then I would just go ahead and create the environment.如果已经安装了 conda,那么我将继续创建环境。 (All this should happen within the same script) I just want to know if it is possible to check the existence of conda within a bash script and then proceed with the rest of the installations? (所有这些都应该发生在同一个脚本中)我只是想知道是否可以在 bash 脚本中检查 conda 的存在,然后继续进行其余的安装?

''' '''

#!/bin/bash
<code_to_check_existence_of_conda_env_here ?>
//If it does not exist, I will run the below code
mkdir -p miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O 
miniconda3/miniconda.sh
bash miniconda3/miniconda.sh -b -u -p ~/miniconda3
conda env update -f my_env.yml

''' '''

The main issue here is that Conda has multiple components.这里的主要问题是 Conda 有多个组件。 Which components are loaded depends on the how Conda is installed and what user the BASH script is executing under.加载哪些组件取决于 Conda 的安装方式以及 BASH 脚本在哪个用户下执行。 I'll try describing the components and hopefully you can decide what is suitable to verify.我将尝试描述这些组件,希望您能决定什么是适合验证的。

Conda Components康达组件

1: Conda Package 1:康达包

The first is a Python package conda , which is installed in the Anaconda/Miniconda/Miniforge base environment.第一个是 Python 包conda ,它安装在 Anaconda/Miniconda/Miniforge基础环境中。 If the base environment is activated , one could test如果基础环境被激活,可以测试

python -m conda

which will give something like这将给出类似的东西

/path/to/python: No module named conda

if it isn't there.如果它不存在。 Otherwise, it outputs the conda entrypoint's documentation.否则,它会输出conda入口点的文档。

2: Conda Entrypoint 2:康达入口点

The entrypoint conda , which acts as a CLI, is located under the condabin directory of the Anaconda/Miniconda/Miniforge installation.充当 CLI 的入口点conda位于 Anaconda/Miniconda/Miniforge 安装的condabin目录下。 When a user runs conda init , a managed section is added to their shell initialization file ( .bashrc for Linux BASH), that includes code to add the condabin to PATH.当用户运行conda init时,会在他们的 shell 初始化文件(Linux BASH 的.bashrc )中添加一个托管部分,其中包含将condabin添加到 PATH 的代码。 This is most likely what OP wants to identify, however, running with the shebang /bin/bash will not load the .bashrc file.这很可能是 OP 想要识别的,但是,使用 shebang /bin/bash运行将不会加载.bashrc文件。 Instead, one should probably be using相反,一个人可能应该使用

#!/bin/bash -l

or或者

#!/usr/bin/env bash -l

Then the entrypoint can be located with然后入口点可以位于

which conda

3: Conda Activate 3:康达激活

Finally, Conda also includes some shell-only functions, which are defined in the aforementioned shell initialization code.最后,Conda 还包括一些纯 shell 函数,这些函数在前面提到的 shell 初始化代码中定义。 This sets up a middleman shell function, also called conda (essentially an alias), which can be viewed with这设置了一个中间人shell函数,也称为conda (本质上是一个别名),可以查看

type conda

This function serves to determine whether the conda (de)?activate commands are being requested, which are pure shell functions, or something that needs to be forwarded to the entrypoint.此函数用于确定是否正在请求conda (de)?activate命令,它们是纯 shell 函数,还是需要转发到入口点的东西。

Recommendation推荐

Were I designing this, I would write an interactive script that checks for #2 ( which conda ) and if that comes up blank then prompt the user to either provide the PATH to the Conda entrypoint (maybe they installed it in a weird place or didn't run conda init ) or install Miniforge.如果我在设计这个,我会编写一个交互式脚本来检查 #2 ( which conda ),如果它出现空白,然后提示用户提供 PATH 到 Conda 入口点(也许他们将它安装在一个奇怪的地方或者没有'不运行conda init ) 或安装 Miniforge。 1 1

I also would not use the base environment to install stuff - that is a bad idea for an end-user, let alone a third-party - and instead create a new environment.我也不会使用基础环境来安装东西——这对最终用户来说是个坏主意,更不用说第三方了——而是创建一个新环境。 I would prompt the user with a specific default environment name, but also provide an option for them to customize.我会用特定的默认环境名称提示用户,但也会为他们提供自定义选项。


[1] Yes, Miniforge, not Miniconda. [1]是的,Miniforge,而不是 Miniconda。 Commercial use of the Anaconda defaults channels now requires a paid license, so better to use the free Miniforge. Anaconda默认频道的商业用途现在需要付费许可证,因此最好使用免费的 Miniforge。

I'm not really good at bash scripting but I would execute a command:我不太擅长 bash 脚本,但我会执行一个命令:

pip3 freeze | grep conda

and based on the output (if exit status was an error or not) install it or directly create the environment.并根据输出(如果退出状态是否错误)安装它或直接创建环境。

在您的机器上运行命令conda list ,安装和未安装时的输出会有所不同,然后在输出上运行 if 语句。

In bash, if we want to check if a software is installed or not, the following check is used.在 bash 中,如果我们要检查是否安装了软件,则使用以下检查。

if dpkg -l $SOFTWARE; then
<Do stuff>

Type conda activate or conda activate env_name键入conda activateconda activate env_name

The first command will activate the base environment if it is already installed.如果已经安装,第一个命令将激活基础环境。 The second command will do activate the user created environment if it is installed.如果安装了第二个命令,它将激活用户创建的环境。

ensureconda确保康达

This sounds like the intended problem that the experimental tool ensureconda was designed to solve.这听起来像是实验工具ensureconda旨在解决的预期问题。 This, however, requires a Python installation with pip:但是,这需要使用 pip 安装 Python:

pip install ensureconda

Here are the command options:以下是命令选项:

$ ensureconda --help
Usage: ensureconda [OPTIONS]

  Ensures that a conda/mamba is installed.

Options:
  --mamba / --no-mamba            search for mamba
  --micromamba / --no-micromamba  search for micromamba, install if not
                                  present
  --conda / --no-conda            search for conda
  --conda-exe / --no-conda-exe    search for conda.exe / conda-standalone,
                                  install if not present
  --no-install                    don't install conda/mamba if no version can
                                  be discovered
  --min-conda-version VERSIONNUMBER
                                  minimum version of conda to accept (defaults
                                  to 4.8.2)
  --min-mamba-version VERSIONNUMBER
                                  minimum version of mamba/micromamba to
                                  accept (defaults to 0.7.3)
  --help                          Show this message and exit.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM