0

When working on multiple projects, I usually create a new conda environment to work with for each project.

For example, I use the following commands to create "projectA" environment:

conda create -n projectA python=3.6 pip

I also use separate tmux session for each project. But when I enter to tmux, and create new pane or new windows, I have to conda activate projectA. I feel it too slow and inconvenience. So, I put those below line into .bashrc file:

conda activate $CONDA_DEFAULT_ENV

It works very good, the FIRST tmux session inherits the environment of the current shell. But I met a problem with when I create SECOND tmux session, it inherit environment variables from the FIRST session, not current shell.

Use case:

  • Step 1: Create shell (base enviroment), activate "projectA" environment.
  • Step 2: Create tmux session 0, it's in "projectA" environment.
  • Step 3: Create a new pane in session 0, it's still in "projectA" environment.
  • Step 4: Create another shell (base environment), activate "projectB" environment.
  • Step 5: Create tmux session 1, it's in "projectA" environment. (while I was hoping it's in "projectB" environment. Why? and how to achieve what I was hoping?

I try to use "printenv | grep CONDA" to debug the environments varibles, at step 4, it produce:

CONDA_SHLVL=2
CONDA_EXE=/home/z/anaconda3/bin/conda
CONDA_PREFIX=/home/z/anaconda3/envs/projectB
CONDA_PREFIX_1=/home/z/anaconda3
CONDA_PYTHON_EXE=/home/z/anaconda3/bin/python
_CE_CONDA=
CONDA_PROMPT_MODIFIER=(projectB) 
CONDA_DEFAULT_ENV=projectB

at step 5, it produce:

CONDA_SHLVL=2
CONDA_EXE=/home/z/anaconda3/bin/conda
CONDA_PREFIX=/home/z/anaconda3/envs/projectA
CONDA_PREFIX_1=/home/z/anaconda3
CONDA_PYTHON_EXE=/home/z/anaconda3/bin/python
_CE_CONDA=
CONDA_PROMPT_MODIFIER=(projectA) 
CONDA_DEFAULT_ENV=projectA
2
  • What commands are you using? – muru Feb 5 '20 at 8:35
  • step1: ctrl alt t, then "conda activate projectA", step 2: tmux @muru – Luan Pham Feb 5 '20 at 8:37
0

How are you creating the new tmux session? If you are creating it from a key binding or from the tmux C-b : command prompt, then tmux has no way to automatically know what the environment variables are inside your running shell.

You can tell tmux what it is by doing something like this in your PS1:

[ -n "$TMUX" -a -n "$CONDA_DEFAULT_ENV" ] && tmux setenv -g CONDA_DEFAULT_ENV "$CONDA_DEFAULT_ENV"

This will update CONDA_DEFAULT_ENV in the tmux global environment to the value from the pane where you most recently used the shell. But the problem with this is that it will then apply to all new panes in any session.

If you want it only be used in new panes in one session, you need to get it into the session environment for that session.

new-window has -e to set an environment variable when the window is created but unfortunately this was not added to new-session, so your best bet is a wrapper script.

So if you have something like this in PS1 to put CONDA_DEFAULT_ENV in a custom environment variable in the tmux global environment:

[ -n "$TMUX" -a -n "$CONDA_DEFAULT_ENV" ] && tmux setenv -g MY_CONDA_DEFAULT_ENV "$CONDA_DEFAULT_ENV"

You could write a script something like this to create a new session and copy the saved variable into the session environment:

S=$(tmux new -dP)

eval `tmux showenv -gs MY_CONDA_DEFAULT_ENV 2>/dev/null`
if [ -n "$MY_CONDA_DEFAULT_ENV" ]; then
    tmux setenv -t$S CONDA_DEFAULT_ENV "$MY_CONDA_DEFAULT_ENV"
fi

if [ -n "$TMUX" ]; then
    tmux switch -t$S
else
    tmux attach -t$S
fi

Then use this script instead of running new-session to create a new session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.