Conda/Mamba environment activation error
2
0
Entering edit mode
9 months ago
Umer ▴ 50

hey all,

I have created a bash script that is below (i have already created environment named "blast" and installed blast in it)

#!/bin/bash
set -e #exit if command fails
conda activate blast
echo "blast help"
blast -help
conda deactivate

when I make my "script.bash" file executable and run it with commands:

chmod +x script.bash
./script.bash

I get the following error/warning at my screen

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

I have tried conda init bash along with restarting my shell source ~/.bashrc and evening close and open terminal again and even reboot terminal. but this error is persistent.

I have been facing this error on 3 different machine

  1. my personal laptop with Ubuntu 22.04 LTS running in WSL-2
  2. My lab PC with Ubuntu 22.04 LTS running in WSL-1
  3. A virtual machine with ubuntu server from university cluster

I have been using miniconda3 on all three systems and getting the same error with both conda and mamba.

shell mamba conda wsl • 3.0k views
ADD COMMENT
0
Entering edit mode

Thank You DBScan Ram Jesse for helpful answers.

ADD REPLY
2
Entering edit mode
9 months ago
DBScan ▴ 300

Have you tried eval "$(conda shell.bash hook)"?

ADD COMMENT
0
Entering edit mode

yes, just ran this said line in terminal. got no output or any error. tried to execute my script again but i got same error.

ADD REPLY
1
Entering edit mode

Did you place it in your script.bash file?

ADD REPLY
0
Entering edit mode

I did add it now to my file. It worked with "conda" (script that contains commands with conda)

Can you kindly explain this command a bit?

Also how can I make it work with mamba mamba activate my_env and mamba deactivate

ADD REPLY
2
Entering edit mode

Can you kindly explain this command a bit?

https://askubuntu.com/a/1254409

Also how can I make it work with mamba mamba activate my_env and mamba deactivate

mamba is probably installed in your base env. You'd need to either conda activate base or use the absolute path to mamba.

ADD REPLY
2
Entering edit mode

To expand on the conda hook thing a bit: conda adds its block of setup commands to your ~/.bashrc file (you were on the right track!) that includes that hook bit that DBScan said you should add to your script. It just doesn't happen automatically because, for scripts, bash runs in a "non-interactive" mode, which means ~/.bashrc isn't loaded for the script itself.

To make it all the more confusing:

  • conda prepends its own directory to your interactive shell's PATH variable, and when you call your script from that shell, the same PATH is used in the script, since the PATH variable is generally set to be "exported." (That's how conda is able to complain at you in your script, rather than bash just saying "command not found.") So you do see some of the conda setup show up in your script, just not all of it.
  • When the "hook" code is evaluated, conda also masks that script version of itself with a set of bash functions, and those functions are not set to be exported. That's the part your script doesn't see.

You can roughly test this out like:

(base) user@whatever:~$ bash --norc
bash-5.2$ conda activate something

CommandNotFoundError: Your shell has not been properly configured [...etc...]

Versus instead:

(base) user@whatever:~$ export -f conda
(base) user@whatever:~$ bash --norc
bash-5.2$ conda activate something
bash: __conda_activate: command not found

(It's trying to call more of its special functions, but I only exported one.) Instead, you can call eval on the bash code conda provides with its hook command like DBScan said, and then you get the whole thing working in one step. And like that askubuntu answer says, you can call conda shell.bash hook directly and see the bash code itself for a better understanding of what it's doing.

ADD REPLY
2
Entering edit mode
8 months ago
Umer ▴ 50

If anyonw faces the same thing ever, I found a better way. If instead of writing it as

conda activate my_env
echo "blast help"
blast -help
conda deactivate

I can write it and run it as

conda run -n my_env blast -help

this works like a charm.

ADD COMMENT
1
Entering edit mode

It's worth noting that conda run evidently works even where conda activate doesn't, so for individual commands like in the question here, this is a drop-in replacement. Nice find.

ADD REPLY

Login before adding your answer.

Traffic: 1602 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6