Max Görner
Max Görner

Reputation: 836

Conda does not set paths when activating environment

When starting a new shell, the PATH environment variable is not configured properly. The directories anaconda3/bin or miniconda3/bin are at second position only, not at first position in the PATH variable. This can be resolved by conda deactivate and activating it again.

This question was asked several times already (e.g. here and here) but the existing questions are either very old or concentrate on the use of source activate env-name. All in all, I found no answer that resolves my problem.

When I start a new shell, the base environment is activated. The relevant snippet from my .bashrc reads like this:

condaexe="/home/$USER/.miniconda3/bin/conda"
condash="/home/$USER/.miniconda3/etc/profile.d/conda.sh"
__conda_setup="$($condaexe 'shell.bash' 'hook' 2> /dev/null)"
# shellcheck disable=SC2181
if [[ $? -eq 0 ]]
then
    eval "$__conda_setup"
elif [[ -f "$condash" ]]
then
    source "$condash"
fi
unset __conda_setup condaexe condash

Then, the PATH variables are defined as follows:

(base)$ echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$ conda deactivate && echo $PATH
/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
$ conda activate base && echo $PATH
/home/user/.miniconda3/bin:/home/user/.local/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project/:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
(base)$

Note that /home/user/.local/bin is contained twice; once before and once after the Miniconda3 directories.

I tried to debug the problem by appending the following snippet to .bashrc:

echo $CONDA_PROMPT_MODIFIER
echo $PATH

This yields

(base)
/home/user/.miniconda3/bin:/home/user/.miniconda3/condabin:/home/user/workspace/my-project:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

which would be perfectly fine but, somehow, is modified after .bashrc. Note that here /home/user/.local/bin is contained only once.

What goes on here? How can I setup Bash/Conda to get a properly defined PATH environment variable?

Upvotes: 3

Views: 5308

Answers (3)

Atoms
Atoms

Reputation: 3

Thanks for this post! That give me hint to debug same issue. I think everyone is different situdation but the idea is same: there is a command like export $PATH change the path in your zshrc or .bashrc which will affect the computer to find the path you want.

Upvotes: 0

Simba
Simba

Reputation: 27588

This is related to the Bash init files. By default, ~/.bashrc is used in an interactive, non-login shell. It won't be sourced in a login shell. Tmux uses a login shell by default. Hence, shells started by tmux skip ~/.bashrc.

default-command shell-command

The default is an empty string, which instructs tmux to create a login shell using the value of the default-shell option.

Init files for Bash,

  1. login mode:
    1. /etc/profile
    2. ~/.bash_profile, ~/.bash_login, ~/.profile (only first one that exists)
  2. interactive non-login:
    1. /etc/bash.bashrc (some Linux; not on Mac OS X)
    2. ~/.bashrc
  3. non-interactive:
    1. source file in $BASH_ENV

The weird interactive, non-login loading requirement confuses people in other situations as well. The best solution is to change the loading requirement of ~/.bashrc as interactive only, which is exactly what some distros, like Ubuntu, are doing.

# write content below into ~/.profile, or ~/.bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

This should be the solution you desire. And I recommend every Bash user setup this in the profile.

References

Upvotes: 3

Max Görner
Max Görner

Reputation: 836

There are three components I missed in the original question which are key to the solution.

First, I run all my shells inside TMux. Second, TMux sources the .profile. Third, in .profile some local directories like the aformentioned ~/.local/bin are blindly prepended to the PATH.

Taken this altogether all the weird behaviour above makes sense. Especially that PATH is correct at the end of ~/.bashrc but not in the shell is obvious now; it got modified in between by ~/.profile.

There are three solutions for this:

  1. deactivating and activating conda manually (temporary workaround)
  2. being more picky about which shells are started inside TMux (very unhandy)
  3. commenting out the manipulations of PATH in ~/.profile

It seems as if one cannot configure TMux to source only ~/.bashrc (reference 1, reference 2), albeit some workarounds exist here too.

Upvotes: 2

Related Questions