6

I have a virtual machine on Azure where after ssh'ing I can run Conda and python alright:

conda activate py36
python some_script.py

To run these commands from my machine through SSH, I need to give the full path to Conda:

ssh ${USER}@${IP} "/data/anaconda/envs/py35/bin/conda activate py36; python some_script.py"

but I get this error:

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

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

Yet, the shell running is bash:

$ sshb ${USER}@${IP} echo $SHELL
/bin/bash

How can I activate a conda environment remotely through SSH?

ginjaemocoes
  • 2,151

6 Answers6

6

Edit: In the process of working this out I found that conda init creates a function called "conda" which mascarades the conda executables. See type conda. Which allows it to set environment variables in the current shell. This is important because it contributes to the problem.

When you run a command via ssh it doesn't run the ~/.bashrc which is what sets up this conda bash function.

So the final answer I have for you is

ssh ${USER}@${IP} ". ~/.bashrc ; conda activate py36; python some_script.py"

. ~/.bashrc simply sources the ~/.bashrc which provides the conda function if you've already run conda init before.

Old Answer

I would suggest you make a shell script with the activation and python code

# job.sh
conda activate py36
python some_script.py

Make runnable with chmod +x job.sh

Then run remotely with the following command:

ssh ${USER}@${IP} bash -l "<relative path to script on remote machine>"

bash -l makes it a login shell which also lets you use your .bash_profile or .bashrc which means you won't need to give the full path.

If you absolutely need a single command use a heredoc.

ssh ${USER}@${IP} bash -l <<HERE
conda activate py36
python some_script.py
HERE

I tried other ways as well but it appears there is some issue with the way that bash preserves environmental changes across command separation. Conda activate sets various environment variables (prefixed with CONDA and the PATH variable).

4

Instead of specifying the path to "conda", it works for me to specify the path to "activate" like this:

ssh [host] "source ~/anaconda3/bin/activate [name of conda env] ; cd [pick a dir] ; [command]"

For [command] try "conda env list" to see which environment is activated. Or you can do "python foo.py".

You may have to adjust the path "~/anaconda3/bin/activate".

mepster
  • 41
  • 1
1

Pieced together from this issue I managed to solve the issue by adding these lines to my .bashrc:

export -f conda 
export -f __conda_activate 
export -f __conda_reactivate 
export -f __conda_hashr 
export -f __add_sys_prefix_to_path 
eval "$(conda shell.bash hook)"

I'm not sure if the exports are still needed with the eval solution but they didn't break anything, so I just kept them.

EDIT: also important to keep in mind, any script using conda in some way needs to be run with bash and not sh.

Tox
  • 111
1

Just an addition to @Maeve Kennedy's answer. Instead of using double quotes, better use single quotes like below

ssh ${USER}@${IP} '. ~/.bashrc ; conda activate py36; python some_script.py'

It allows you to execute any bash command after activating conda environment remotely. Something like below;

ssh ${USER}@${IP} '. ~/.bashrc ; conda activate py36; echo $CONDA_PATH'

with double quotes it doesn't work.

Abhishek
  • 11
  • 2
1

This is my current approach:

"ssh.exe" -i "myRSAfile" -t ${USER}@${IP} "~/.pycharmrc"

And the file "~/.pycharmrc" has the content:

#!/bin/bash
cd /tmp/pycharm_project_617
source /home/yode/anaconda3/etc/profile.d/conda.sh
conda activate polydetect
exec /bin/bash

The only drawback is that it doesn't show the name of the current conda environment in the prompt, but the environment is already active and available. I mean when you conda info -e you will see the star sign

yode
  • 733
1

The other answers didn't work for me. This did:

ssh ${USER}@${IP} -t 'bash -ic "conda activate py36 && conda info"'

This just starts an interactive shell first before running anything.

alvitawa
  • 143
  • 4