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
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*')
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')
Related
I am trying to follow along with a book that for some of the chapters requires the line
export PATH="$PATH:$HOME/scripts"
be added to a .bash_profiles in the home directory. Probably, after peaking later in the book it is to launch scripts located in that folder quickly from the terminal without specification of path of that file, it seems useful so I would like to do it.
Although, since I have previously installed conda on my mac, it apparently changed something and addition of that line did not alter the output of echo $PATH
.bash_profiles looks now like this:
# added by Anaconda3 2019.10 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/Users/eshauchuk/opt/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/Users/eshauchuk/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/eshauchuk/opt/anaconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
\export PATH="$PATH:$HOME/scripts"
else
\export PATH="/Users/eshauchuk/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
export PATH="$PATH:$HOME/scripts"
# <<< conda init <<<
I have added the mentioned line to the near end of the .bash_profile now and also went to the found in this files conda files to try add it there with no change for now.
What can I do to make it work
Check this comment:
# !! Contents within this block are managed by 'conda init' !!
So try to put your line after the block like this:
# <<< conda init <<<
export PATH="$PATH:$HOME/scripts"
I have miniconda 4.8.3 + MacOS Catalina 10.15. I can manually activate the conda environment in the terminal and start a spyder session.
$ ~/miniconda3/bin/conda activate py3
$ ~/miniconda3/bin/conda info | grep "active environment"
$ spyder &
When I put the above in a script, run_spyder.sh it's not working, and it complains about "CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'."
#!/bin/bash
# run_spyder.sh
~/miniconda3/bin/conda activate py3
~/miniconda3/bin/conda info | grep "active environment" # still print base
# spyder &
I tried alternatives like bash -i ./run_spyder.sh, or source ./run_spyder.sh, or adding ~/miniconda3/bin/conda init bash, none of them work.
Shell is still bash, no .bashrc, in .bash_profile this is the script automatically generated by miniconda installation
# .bashrc_profile
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('~/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "~/miniconda3/etc/profile.d/conda.sh" ]; then
. "~/miniconda3/etc/profile.d/conda.sh"
else
export PATH="~/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
The conda activate function is a shell function that is typically defined in the initialization file for a shell when the session starts (e.g., in the .bash_profile). The conda init function merely adds code to such initialization files, but will not actually source the code it adds. Hopefully, that clarifies the difficulty with what was tried in the question (and comments).
Instead, try directly sourcing the code that Conda uses. Something like:
#!/bin/bash
source ~/miniconda3/etc/profile.d/conda.sh
conda activate py3
conda info | grep "active environment"
spyder &
Another option is to have the bash (or zsh) session launch in login mode (i.e., runs the initialization files for the current user).
#!/usr/bin/env bash -l
conda deactivate # <- may not be needed, but didn't work for me without
conda activate py3
conda info | grep "active environment"
spyder &
However, note that in this latter case I find I need to include a conda deactivate first, in order for the conda activate to properly prioritize the Python in the env on PATH.
I downloaded the Anaconda individual edition 2020-2 graphical installer. And then by using the graphical wizard I installed Anaconda successfully in my macbook. I checked this by running the command conda --version it was giving the result of conda 4.8.2.
Next day when I opened the terminal and tried executing conda commands surprisingly I was getting command not found error. I checked the PATH by executing echo$PATH and found that Anaconda directory is not included in the path. I tried to add anaconda by running the following command:
export PATH="usr/local/bin:usr/bin:/bin:usr/sbin:/sbin:/opt/anaconda3/bin"
After this conda commands were working on the same session but it was not when a new terminal window was opened.
To fix this I opened the .bash_profile file in my home directory in vi editor and found that the following lines were already there in the file:
bash-3.2$ cat .bash_profile_bk
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/anaconda3/etc/profile.d/conda.sh" ]; then
. "/opt/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/opt/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
So I renamed the file to .bash_profile_bk by the following command
$mv .bash_profile .bash_profile_bk
Then created a new .bash_profile and added the export PATH line as follows
$vi .bash_profile
in vi editor:
export PATH="usr/local/bin:usr/bin:/bin:usr/sbin:/sbin:/opt/anaconda3/bin"
I checked the content of the file:
bash-3.2$ cat .bash_profile
export PATH="/usr/bin:usr/local/bin:/bin:usr/sbin:/sbin:/opt/anaconda3/bin"
exit the terminal session and then opened another terminal window. Typed the conda command but again it was not working:
bash-3.2$ conda --version
bash: conda: command not found
Can somebody suggest if I'm missing something? any help will be greatly appreciated.
I am using conda 4.6.8 to test a python package in a conda env on Travis CI. I want to replace my old source activate ENVNAME line with the new conda activate ENVNAME command in my Travis CI configuration. If I run this on Travis:
>>> conda update -n base conda
>>> conda init
no change /home/travis/miniconda/condabin/conda
no change /home/travis/miniconda/bin/conda
no change /home/travis/miniconda/bin/conda-env
no change /home/travis/miniconda/bin/activate
no change /home/travis/miniconda/bin/deactivate
no change /home/travis/miniconda/etc/profile.d/conda.sh
no change /home/travis/miniconda/etc/fish/conf.d/conda.fish
no change /home/travis/miniconda/shell/condabin/Conda.psm1
no change /home/travis/miniconda/shell/condabin/conda-hook.ps1
no change /home/travis/miniconda/lib/python3.7/site-packages/xonsh/conda.xsh
no change /home/travis/miniconda/etc/profile.d/conda.csh
modified /home/travis/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
How can I "close and re-open" my shell on Travis? Because otherwise I cannot activate my conda environment:
>>> conda create -n TEST package_names
>>> conda activate TEST
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'.
The command "conda activate TEST" failed and exited with 1 during .
Your build has been stopped.
Not sure it is currently supported as the official doc still uses source in travis.yml.
What does conda init do?
This new command should harmonize the way users setup their shells to be able to call conda activate.
Actually, if you run conda init --dry-run --verbose you will see that it tries to source conda.sh from your ~/.bashrc (assuming you're running Bash, from info mentioned in your question).
And conda.sh will define a conda() function that will catch a few commands among which activate and deactivate and dispatch to $CONDA_EXE:
conda() {
if [ "$#" -lt 1 ]; then
"$CONDA_EXE"
else
\local cmd="$1"
shift
case "$cmd" in
activate|deactivate)
__conda_activate "$cmd" "$#"
;;
install|update|upgrade|remove|uninstall)
"$CONDA_EXE" "$cmd" "$#" && __conda_reactivate
;;
*) "$CONDA_EXE" "$cmd" "$#" ;;
esac
fi
}
So unless this function is defined in your local shell, you won't be able to call conda activate.
Hint on a solution? (not tested for Travis CI)
The only hint I can suggest is to try source $(conda info --root)/etc/profile.d/conda.sh and then conda activate. This should do roughly the same as conda init assuming you are using Bourne shell derivatives.
For csh there is $(conda info --root)/etc/profile.d/conda.csh, and for fish there is $(conda info --root)/etc/fish/conf.d/conda.fish
Note: although not tested for Travis CI, this solution works for me from bash. Of course, the conda executable should be found in PATH for conda info --root to work properly.
Here's the response I keep getting whenever I try to launch Jupyter notebook:
Users-MacBook:~ user$ jupyter notebook
-bash: jupyter: command not found
And these are the current content of my .bash_profile:
# added by Anaconda3 2018.12 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/Users/user/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"
export PATH
export PATH
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/Users/user/anaconda3/etc/profile.d/conda.sh" ]; then
. "/Users/user/anaconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="/Users/user/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda init <<<
Am really frustrated by it. Can I rewrite the bash_profile again or could I just download the .bash_profile somewhere; that means I would have to copy it from someone. I've not found anything anywhere.
Rename your .bash_profile to something else so it won't mess with your console. To remove it use the full path in command, for example
/bin/mv ~/.bash_profile ~/bad_bash_profile
Then your next new session will start with whatever is default for all users and you can check what's wrong and fix it.
If you want more info on what's wrong, please post the contents of your $PATH variable as commented by glenn jackman.