conda activate the specific env in nextflow

52 views Asked by At

The version of nextflow is N E X T F L O W ~ version 23.10.1 However, according to the official website command:

nextflow Copy code process foo { conda '/path/to/an/existing/env/directory'

''' your_command --here ''' } I correctly set the conda environment path in my process, but it did not activate successfully because my command: python -c "import sys; print(sys.version)" did not display the correct version.

However, if I directly set in the script:

bash Copy code source /path/to/anaconda3/etc/profile.d/conda.sh conda activate ${params.condaEnv1} # This variable value is my environment path it succeeded. Why is this?

1

There are 1 answers

0
dthorbur On

I'm assuming you built a docker container to use with nextflow, but it's not clear.

conda activate in the .bashrc are intended for interactive shells, which is not how nextflow interacts with them. There are a few tricks in previous posts on here, but the one I like is to add the conda env to PATH.

From my nlrexpress dockerfile:

ENV PATH /opt/conda/envs/nlrexpress/bin:/usr/local/src/nlrexpress:$PATH

You can also use a python shebang that points to the conda version of python in some use cases and use the python script as an executable, but I think this is less flexible.

You can then test if it works by invoking something like this:

docker run container python3 --version

Which should return the conda installed python. NB, running docker run -it will create an interactive shell and invoke the .bashrc file.