25

Ok, so I am in my anaconda environment and I ran which python. I get

/home/comp/anaconda3/envs/env1/bin/python

Now if I start tmux, then run source activate env1, then which python, I get

/home/comp/anaconda3/bin/python

even though I do have my environment activated. How can I make anaconda see the same path inside tmux ?

0xSheepdog
  • 2,750
leniX
  • 251
  • 1
  • 3
  • 3

10 Answers10

32

The solution seems to be to deactivate the conda environment, then start tmux, then reactivate the environment inside tmux.

leniX
  • 321
  • 2
  • 2
8

I had the same problem, but I really didn't like any of the solutions since they involved deactivating and activating Conda every time I load into tmux. So instead, I added what is below to my .tmux.conf:

IMPORTANT EDIT: This code works for me, as I'm currently running a zsh shell and this is where my settings are stored. Your standard shell might be different, to find your shell location use the command echo "$SHELL" and replace the /bin/zshin my answer with your own shell path.

set -g default-command "/bin/zsh"

Once done, just resource your .tmux.conf file and the changes should be activate. This should allow tmux to load whatever settings you have in your .bash_profile including Conda.

  • 2
    Please don't assume that his shell is zsh (he doesn't mention it). If people without it use your answer they may have issues. Perhaps you should also explain a little more on why does it solve the issue, like what is -g there, what the default value does, etc... – Zip May 14 '20 at 01:02
  • Good point, I didn't think about people with other shells at all. I'll change up the answer to clarify that. – Siddarth Raghuvanshi May 14 '20 at 17:17
  • 3
    A more general answer might be set -g default-command "${SHELL}" (I am not sure whether $SHELL is supposed to always be set by shells, but I expect this to often work). – Eric O. Lebigot Jan 30 '21 at 16:27
  • Note to anyone trying this: without killing my tmux server and all running sessions, this only worked for me when I created a new session, but not when I created new windows or panes within that session. Upon restarting everything this worked great. – Stephen Shank May 17 '22 at 18:09
6

This behaviour is caused by TMux sourcing ~/.profile instead of ~/.bashrc. My ~/.profile is this:

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

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

You can see that first ~/.bashrc is sourced and then ~/bin and ~/.local/bin are prepended. As I experienced myself, this causes conda to hickup.

The solution would be to comment out the two blocks manipulating PATH in ~/.profile.

Edit (2019/09/24): An even better seems to be to configure TMux such that it does not spawn a login shell but just a normal one. See the answers for the linked question.

3

Running:

conda activate env1

Instead of:

source activate env1

When inside tmux worked for me.

2

The following happens to me after starting a Tmux session (without conda having any active env).

When I first do inside the Tmux session:

conda activate myEnv

I get

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

If instead I do:

source deactivate
conda activate myEnv

Everything works just fine. which python points to the correct path.

mdev
  • 121
2

I find that tmux will always call the profile for your shell, not just the rc. So if you are using bash like I do, it will call /etc/profile, which will have a call to path_helper.

In order to fix this, change /etc/profile to:

if [[ -z $TMUX ]] && [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

If you are using bash, also change any export PATH=$PATH:/foo in .bashrc to

if [[ -z $TMUX ]]; then
  export PATH=$PATH:/foo
fi

then you restart the terminal(for example Iterm). Everything should be good!

H.Li
  • 269
2

You need to disable tmux from spawning login shell as described in tmux is causing anaconda to use a different python source

ikamen
  • 121
1

There is a fully automated way to do it so you don't have to manually deactivate and activate the environment each time. It's a combination of some of the other answers here. The first step is to open your .tmux.conf file and find the line set -g default-command and add to it so that it says set -g default-command "${SHELL}" (Note: It might also say set-option -g default-command; set is an alias of set-option, so either is fine).

Then, in your initialization scripts, after the conda initialize section that conda automatically adds, you'll need to deactivate and then re-activate your conda. Here's one way to do that:

I recommend people use a .profile file to customize their environment. To do that, you can source .profile from your .bashrc/.zshrc file (depending on which shell you use). Then, take the conda initialization script that conda added and put it in .profile. Then, after this, add conda deactivate and conda activate my_conda_env. Following the conda initialization, it should look like:

# <<< conda initialize <<<
conda deactivate
conda activate my_conda_env

It doesn't work without the conda deactivate step, so be sure to include that.

You can test that this is working by entering which python after you enter tmux and ensuring that it points to the correct python in your conda environment.

jss367
  • 195
0
nano ~/.bash_profile

Add the following lines:

source deactivate env1
source activate env1

worked for me.

0

When running conda activate before, I was being told 'CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run...'

The fix for me was a simplification of the answer of @jose.marcos.rf (I lack commenting privileges).

All that was needed was to source the conda.sh file from my .bash_profile file. In my .bash_profile I have the following:

if [[ ! -z $TMUX ]]; then
    # Enable `conda activate` and such within Tmux
    . "/usr/local/anaconda3/etc/profile.d/conda.sh"
fi

Then, opening a new Tmux session and running conda activate works as expected.

Alex W
  • 101