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:


at step 5, it produce:

  • What commands are you using?
    – muru
    Feb 5, 2020 at 8:35
  • step1: ctrl alt t, then "conda activate projectA", step 2: tmux @muru
    – Luan Pham
    Feb 5, 2020 at 8:37
  • I have the exact same problem, the tmux's behavior here is a complete non-sense...
    – None
    Jan 23, 2022 at 10:15
  • 1
    @Alexis, wow, it's about two years. I've switched to the "venv" package. Instead of adding "conda activate $CONDA_DEFAULT_ENV" into .bashrc file, I've added "source env/bin/activate" instead. Good lucks :v
    – Luan Pham
    Jan 24, 2022 at 11:08

1 Answer 1


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:


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

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

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

  • Hoes does tmux know the environment for the first created session then?! Doing all that doesn't make any sense since tmux is correctly able to catch all the env vars for the first session (ProjectA) and then keep it for the next created sessions.
    – None
    Jan 23, 2022 at 10:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .