Michael
Michael

Reputation: 11

Calling An Anaconda Environment from MATLAB: Conda Command Not Found

I want to call a Python script I created in its own Anaconda environment and wanted to call the script from Matlab 2020a. However, when I try to activate the environment from Matlab, I get an error message:

system('conda activate *name_of_environment*')
/bin/bash: conda: command not found

I installed the newest version of anaconda3 (2020.02) on a Ubuntu 18.04 machine and, as recommended, didn't add conda to bashrc but added the conda.sh directory instead as recommended here:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/michael/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/michael/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/michael/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/michael/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

# export PATH="/home/michael/anaconda3/bin:$PATH"  # commented out by conda initialize

#Enable conda to be called from bash
source /home/michael/anaconda3/etc/profile.d

However, I can't find an explanation how to run conda from Matlab otherwise. Am I missing something?

Thanks a bunch, and best,

Michael

Upvotes: 1

Views: 2457

Answers (2)

sebway
sebway

Reputation: 33

I know it is too late, but maybe the best way to run a python script using a conda environment is to call the python executable associated with that environment directly:

system('~/anaconda3/envs/<name_of_environment>/bin/python your_script.py')

Upvotes: 1

Bayou
Bayou

Reputation: 3441

Let me elaborate my comment it in an answer.

Binaries are found trough the PATH environment variables. The location of conda is not in that variable. Therefore you should either add it to your PATH variables (or un-comment it that script at your notification).

Example:

$ export PATH="$PATH:/home/michael/anaconda3/bin/"
$ ./yourscript.sh

But it also can be that the PATH variable isn't copied through system(), which I guess executes the script in a new shell. In this case, you should execute it as:

system('/home/michael/anaconda3/bin/conda activate *name_of_environment*')

Upvotes: 1

Related Questions