Add conda environment info to terminal prompt - bash

(I'm using anaconda on a MacBook)
By default conda adds the environment info to the comand prompt as follows:
$ source activate my_env
(my_env) $ source deactivate
$
This can be switched off and on using
conda config --set changeps1 (true|false)
Since my terminal prompt is already customised I'd like to add the env info in a different way, but don't know how to exactly.
Right now I'm using the two commands sacand dac in my .bash_profile file to activate and deactivate envs and therefore did this amateurish attempt adding env_var:
env_var=""
#activate env (default env = my_env)
sac() {
if [ -z $1 ];
then
ENV="my_env"
else
ENV="${1}"
fi
source activate ${ENV}
env_var="${ENV}"
}
#deactivate env
dac() {
source deactivate
env_var=""
}
env_info() {
if [[ ${env_var} == "" ]]
then
echo ""
else
echo "in ${env_var}"
fi
}
PS1="\u "
PS1+="$(env_info) \$";
Which is not working (my bash knowledge is only rudimentary sorry...).
env_info always stays "" no matter wether I call sacor dacin the terminal or not.
Question1: Why is the code not working?
Question2: Or is there maybe another way to get the current env-info in a - for this purpose - useful format?
conda info --envs returns to much info...

The method suggested in the comment from darthbith works very well. The variable $CONDA_DEFAULT_ENV is exactly what I was looking for:
>>> source activate myEnv
>>> echo $CONDA_DEFAULT_ENV
myEnv

To add to the answer by A.Wenn, this is what I added to my custom prompt
PS1=""
# Add conda environment to prompt
if [ ! -z "$CONDA_DEFAULT_ENV" ]
then
PS1+="($CONDA_DEFAULT_ENV) "
fi

Related

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)

conda activate on Travis CI

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.

Unable to set the PATH variable for jdk

I have installed sun-java in archlinux kde by first building the package and then installing it. This is the way the environment variables are set in my machine:
file: /etc/profile
# /etc/profile
#Set our umask
umask 022
# Set our default path
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin"
export PATH
# Load profiles from /etc/profile.d
if test -d /etc/profile.d/; then
for profile in /etc/profile.d/*.sh; do
test -r "$profile" && . "$profile"
done
unset profile
fi
# Source global bash config
if test "$PS1" && test "$BASH" && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
# Termcap is outdated, old, and crusty, kill it.
unset TERMCAP
# Man is much better than us at figuring this out
unset MANPATH
and file: /etc/profile.d/jdk.sh
export J2SDKDIR=/opt/java
export PATH=$PATH:/opt/java/bin:/opt/java/db/bin
export JAVA_HOME=/opt/java
export DERBY_HOME=/opt/java/db
what I understand from this is, jdk path should be set in the path environment variable but its not. But the attribute $JAVA_HOME is set correctly. Any reasons why am I facing this problem?
/etc/profile and /etc/profile.d are processed only for login shells, so unless you're doing ssh into the machine where java is installed you won't get those variables.
To have them locally (e.g. when you open an xterm on a workstation) put them in the file /etc/bash.bashrc.
Hope this helps.
Actually, it was a silly mistake on my part. I am using zsh shell. So I was required to put:
export PATH=$PATH:$JAVA_HOME/bin
in .zshrc file instead of .bashrc.

Resources