Why piping a conda command into bash doesn't work ?
3
1
Entering edit mode
2.6 years ago

The package upsetr was installed with conda. It works in my terminal

conda activate upsetr
conda deactivate

I was trying to invoke conda in a Makefile , something like

SHELL=/bin/bash
test:
      conda activate upsetr &&  conda deactivate

but it doesn't work:

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:

I can reproduce this error by piping the command into bash

$ echo 'conda activate upsetr && conda deactivate' | bash

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Why does it fail ?

makefile bash conda • 4.0k views
ADD COMMENT
1
Entering edit mode

Is the conda initialization stuff in your .bash_profile or .bashrc and executed by default?

Update: Hmm, I guess I never tried echoing this to bash, it indeed doesn't work. There's probably some bash magic behind that.

ADD REPLY
0
Entering edit mode

it's in bashrc

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
ADD REPLY
0
Entering edit mode

Yeah, but:

echo "declare -F | grep conda" | bash

Returns nothing, so somehow bash is behaving differently.

ADD REPLY
0
Entering edit mode

correction I see it in both files bashrc and bash_profile

wrong command

ADD REPLY
4
Entering edit mode
2.6 years ago

Non-interactive shells do not run .bashrc.

So you could e.g. add the conda init stuff to $BASH_ENV, or alternatively make your sub-shell interactive (with all the side-effects that implies) by piping into … | bash -i or in the Makefile use SHELL = /bin/bash -i.

For the (GNU) Makefile, probably the best bet would be to avoid those side-effects via

SHELL = /bin/bash
export BASH_ENV = $(HOME)/.conda.bash_env

Where ~/.conda.bash_env is a one-liner: eval "$(/path/to/bin/conda shell.bash hook)"

Finally, you will need to ensure each command in a Make recipe that uses conda is actually executed via $(SHELL) rather than spawned directly by make via exec(2)/etc. The easiest way to do that is to make sure that each of those command lines contains at least one shell meta-character, e.g., just by putting a semicolon on the end.

As your conda activate … commands will be setting $PATH for the following commands in the recipe, this will somewhat take care of itself as you will want to ensure that both commands are run by the same shell, either using .ONESHELL or the usual syntax for shell script snippets:

analyse:
    conda activate upsetr; \
    upsetr foo bar
ADD COMMENT
0
Entering edit mode

Thank you, specifying

SHELL=/bin/bash -i

in the makefile worked , as well as

 echo 'conda activate upsetr && conda deactivate' | bash -i && echo "done"
ADD REPLY
0
Entering edit mode

bash -l works too.

ADD REPLY
2
Entering edit mode
2.6 years ago
Joel ▴ 20

Everything define in your .bashrc or .profile won't work when using | bash

If you really want to execute on a new bash instance try

echo 'source .bashrc && conda activate' | bash

Otherwise if you to run it on your current shell try

eval $(echo 'conda activate')
ADD COMMENT
2
Entering edit mode
2.6 years ago

I have learned quite a few techniques above, let me share what I do to avoid sourcing .bashrc on every system. There is another conda specific script at ~/miniconda3/etc/profile.d/conda.sh that seems to be more appropriate for the purpose.

The shell script below runs in whatever environment you pass to it as a parameter and it sources the conda.sh script.

# Conda prefix
ENV=$1

# Load conda specific commands.
source ~/miniconda3/etc/profile.d/conda.sh

# Activate the environment.
conda activate $ENV

# Print conda prefix
echo PREFIX:$CONDA_PREFIX

of course, conda may choose to change the content of conda.sh at any time so we need to keep that in mind.

ADD COMMENT

Login before adding your answer.

Traffic: 1217 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