Adding conda to the PATH on macos Catalina - macos

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.

Related

Editing .bash_profiles with conda installed

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"

conda activate command not working on mac

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.

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
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')

Activating conda environment from bash script

I would like to change my conda environment from a bash script.
I want to run bash script_yxz, where 'script_xyz' is like:
#!/bin/bash
conda activate my_env
and switch to my_env.
This already works if I run source script_yxz.
But I have the problem that I am not able to 'source' on remote machines with 'sshpass'.
To better understand my purposes, my goal is to run on my terminal
sshpass -p "password" ssh -o user#server "bash script_xyz"
and changing the environment on the server.
This is why I need to use bash instead of source.
I have read a lot of solutions on various forums but none of them works.
seems like the conda script is not imported by default so this should fix it
source ~/anaconda3/etc/profile.d/conda.sh
conda activate <env>
#!/bin/bash
eval "$(conda shell.bash hook)"
source ~/anaconda3/etc/profile.d/conda.sh
conda create -n testing python=3.10 -y
conda activate testing
python --version
#output
You can use bash,zsh or any shell aliases for this purposes. You just add
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
line into the .bashrc,.zshrc or .any_other_shell_rc.
"N.B. My environment name is MyPy38". So,replace it according name as well as the path /home/$USER/anaconda3.
Also you can create separate file for aliases. Just create a file called .bash_aliases and add
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
lines to .bashrc,.zshrc or .any_other_shell_rc and keep the command
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
into the .bash_aliases. Now, source ~/.zshrc ~/.bashrc or just close and open a new terminal. Run the command my_conda and BOOM!
Also, you can add some other aliases for jupyter-notebook jupyter-lab spyder etc. like
# Just activate my conda
alias my_conda='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38'
# Open Jupyter Notebook in my Env
alias my_jupn='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-notebook'
# Open Jupyter Lab in my Env
alias my_jupl='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && jupyter-lab'
# Open Spyder in my Env
alias my_spyder='source /home/$USER/anaconda3/bin/activate && conda activate MyPy38 && spyder'
To confirm active environment name python code
import sys
print(sys.executable)

I have broken my PATH on bash_profile and nothing works, can't even launch Jupyter notebook

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.

Resources