Conda activate not persistent in command

Our workflow has a number of jobs that depends on conda, so I thought to pull conda related items into a command however, activating the environment doesn’t persist into the actual job itself. For example

commands: 
conda_stuff:
    description: "setup and activate conda environment"
    steps:
      - run:
          command: |
            ...
            conda activate my_env

The job would be:

my_job:
    ...
    steps:
      ...
      - conda_stuff:
      - run:
          command: |
            conda env list

and the output would be:

# conda environments:
#
base                  *  /home/circleci/miniconda
my_env            /home/circleci/miniconda/envs/my_env

where base is activated.
Is there something I’m not understanding about how commands work?

Hi @SLinAppliedBiomath and welcome to our developer’s forum!

To recap, you have commands that each depend on a conda environment, but the environment is not persisted between run steps. Is that correct?

Each run step actually runs in its own subshell. So the environment that you activated in one run step will not be activated in the next. As such, you may need to run “conda activate my_env” in each run command.

Can you try that and see if you are able to use your conda environment that way?

Thank you!

1 Like

Our Workflow has a job that runs the following. Event if I add the conda activate in every step it still doesn’t recognize it.

#!powershell.exe
conda activate pyxperiflow
conda env list
pytest .\xperiflow\source\tests\database\

conda environments:

base * C:\tools\miniconda3
pyxperiflow C:\tools\miniconda3\envs\pyxperiflow

pytest : The term ‘pytest’ is not recognized as the name of a cmdlet, function, script file, or

Interesting use case. I run a self hosted runner. The powershell in the runner is already configured to support conda. I have conda in the path and ran conda init already.

Once I started ran conda init and refreshenv once as a step in a job in a circle ci config then the commands posted above started to take action.