Reputation: 974
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?
Upvotes: 22
Views: 11370
Reputation: 2174
Somehow all of these answers failed me. I ended up using conda run
instead of activating an environment. That allowed me to run pytest without activating the environment
- conda run -n <environment-name> python -m pytest <addl-pytest-args>
Upvotes: 4
Reputation: 90276
Another possibility that you might find more succinct and elegant: source directly the code needed for conda to run run with bash. This also has the effect of adding conda to the PATH, if it's not the case.
This is done with
- source <anaconda_root>/etc/profile.d/conda.sh
- conda activate myenv
(Stolen from https://stackoverflow.com/a/60523131/11343)
Upvotes: 4
Reputation: 1553
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
Upvotes: 8
Reputation: 809
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.
Upvotes: 23