简体   繁体   English

在一个进程中运行来自 bash 脚本的两个命令 (conda)

[英]Running two commands from bash script in one process (conda)

I'm trying to write a bash script that includes deactivating and removing a conda environment.我正在尝试编写一个 bash 脚本,其中包括停用和删除 conda 环境。 Here's an example, remove_env.sh :这是一个示例remove_env.sh

#!/bin/bash

# Get the conda command available in bash
eval "$(conda shell.bash hook)"

# Deactivate environment
conda deactivate

# Remove environment
conda remove --name my_env --all --yes

The environment must be deactivated in order to remove it.必须停用环境才能将其删除。

Unfortunately, this doesn't work.不幸的是,这不起作用。 I perform this in the terminal:我在终端中执行此操作:

$ conda activate my_env
$ ./remove_env.sh

CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again

I think the issue has to do with forking - essentially, the environment gets deactivated in one process, but then the remove call is run in another process, which doesn't have the environment deactivated.我认为这个问题与分叉有关——本质上,环境在一个进程中被停用,但随后 remove 调用在另一个进程中运行,该进程没有停用环境。 But I'm not entirely sure.但我不完全确定。

Some notes:一些注意事项:

  • I can't use source remove_env.sh - I must be able to use ./remove_env.sh我不能使用source remove_env.sh - 我必须能够使用./remove_env.sh
  • I've tried this with no success:我试过这个但没有成功:
#!/bin/bash

# Get the conda command available in bash
eval "$(conda shell.bash hook)"

# Deactivate and remove environment
conda deactivate && conda remove --name my_env --all --yes
  • I call the command conda activate my_env in my ~./bashrc我在~./bashrc中调用命令conda activate my_env
  • I can't use aliases - it must be a bash script我不能使用别名 - 它必须是 bash 脚本

Thanks Jonathan for the answer in the comments.感谢乔纳森在评论中的回答。 You're totally right, I completely overlooked that blue note in the conda manual.你完全正确,我完全忽略了 conda 手册中的蓝色注释。 I was able to do this:我能够做到这一点:

#!/bin/bash

# Get the conda command available in bash
eval "$(conda shell.bash hook)"

# Activate the environment
conda activate my_env

# Deactivate environment
conda deactivate

# Remove environment
conda remove --name my_env --all --yes

I think it works whether you conda activate with or without arguments.我认为无论您使用或不使用 arguments 进行 conda conda activate ,它都有效。

Alternatively, using Conda's run tool, one could avoid manual activation.或者,使用 Conda 的run工具,可以避免手动激活。 That is, something like也就是说,像

#!/usr/bin/env conda run bash

conda env remove -n my_env -y

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

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