How do I use `conda activate` in Cylc?

When I try this:

[meta]
    title = "Download Data"

[cylc]
    UTC mode = True

    [[parameters]]
        # A list of the sources
        source = a, b

[scheduling]
    initial cycle point = 20150214T12Z
    final cycle point = 20150215T12Z

    [[dependencies]]
        [[[PT1H]]]
            # Repeat every one hours starting at the initial cycle point.
            graph = """
                download<source>
            """

[runtime]
    [[download<source>]]
        script = """
            conda activate download_env
            download $CYLC_TASK_PARAM_source -dt $CYLC_TASK_CYCLE_POINT;                                                                                                                                                   """

I get ~/anaconda3/etc/profile.d/conda.sh: line 55: PS1: unbound variable

Any ideas why?

Maybe your environment doesn’t have the PS1 variable, which is used by the Conda initialization script.

Take a look at the workarounds in this issue: [conda 4.6] activate fails due to unbound PS1 in bash strict mode · Issue #8186 · conda/conda · GitHub

Bruno

1 Like

Yeah that’s a long-standing bug in the conda activation script.

It surfaces in Cylc task job scripts because they use set -u to make it illegal to reference unbound variables, which is a common source of error in shell scripts. You can make it happen in the terminal too, like this:

[drugs-and-money]$ unset PS1  # (i.e. nuke the command prompt!)
set -u
conda activate toast
-bash: PS1: unbound variable

As @kinow suggests, you can make sure PS1 is set in your environment.

Or, you can turn off unbound variable checking temporarily, in your job script:

set +u
conda activate my-env
set -u

Note also you need to be careful about changing your Python environment in Cylc 7 task job scripts, because task status message uses the Cylc Python library. So if you conda environment includes its own Python version, deactivate it again after use:

conda activate download_env
download stuff
conda deactivate
...

… or do the conda stuff in a subshell or external script, so that it doesn’t affect the parent job script environment.

Hilary

This will not be a problem in Cylc 8, which runs all user-defined job scripting in a subshell.