Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activating conda environment during gitlab CI

My .gitlab-ci.yml file looks like this:

anomalydetector:
  image: continuumio/miniconda:4.7.10
  stage: build
  tags:
    - docker
  script:
    - conda env create -f environment.yml
    - conda activate my-env
    - pytest tests/.

On Gitlab, this job starts fine, and the logs read

$ conda env create -f environment.yml
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.10
  latest version: 4.7.11

Ok, so I'm using a conda version later than 4.4, so conda activate should work. However, the job fails with the following:

# To activate this environment, use
#
#     $ conda activate my-env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

$ conda activate my-env

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

    $ conda init <SHELL_NAME>

I have then tried editing my .gitlab-ci.yml file so that there is a command

conda init bash

but then get the message

==> For changes to take effect, close and re-open your current shell. <==

How can I activate my conda environment in the gitlab CI process?

like image 229
EuRBamarth Avatar asked Sep 02 '19 09:09

EuRBamarth


People also ask

How can I activate conda environment automatically?

Reload Window from Command Palette, select base:conda as python interpreter then press Ctrl+Shift+` to open a new integrated Terminal, conda environment should be activated automatically in it.

How do I activate a conda environment in my Bashrc?

you can simply add the anaconda bin folder (eg.: ~/anaconda3/bin ) to the system PATH and then source activate ENV_NAME in your ~/. bashrc or ~/.

What does conda activate do?

You activate (deactivate) an environment using the conda activate ( conda deactivate ) commands. You install packages into environments using conda install ; you install packages into an active environment using pip install . Use the conda env list command to list existing environments and their respective locations.


2 Answers

Similarly to Tommy's answer, this needs to be done for the Windows Powershell as well. Contrary to bash conda activate myenv does not fail in the powershell. It just has no effect (i.e. the environment is not switched) without calling conda init powershell which makes it even more awkward. Reloading the profile in the powershell is more complicated since there are six of them [1]. I used:

 - conda create --name myenv
 - conda init powershell
 - "if (test-path $PROFILE.CurrentUserAllHosts) { & $PROFILE.CurrentUserAllHosts}"
 - conda activate myenv 

Why Conda uses the $PROFILE.CurrentUserAllHosts profile has been asked in an issue [2].

references:

[1] https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/

[2] https://github.com/conda/conda/issues/8608

like image 134
cweigel Avatar answered Oct 05 '22 17:10

cweigel


conda init touches the .bashrc file. To reinitialize the shell you can source it:

  - conda create --name myenv
  - conda init bash
  - source ~/.bashrc    # <- !!!
  - conda activate myenv 

Whether this is better or worse than source activate myenv is a separate discussion, I guess.

like image 43
Tommy Avatar answered Oct 05 '22 15:10

Tommy