0

Suppose my current activated conda environment is py314 on Linux. When I start a new bash environment from the current one, no matter via command bash, screen, or start vscode from command line, the new bash terminal will be set back to base again.

I hope that the conda environment in new bash session should be the same as its parent's, so that I don't have to manually change it to the right one every time.

This is extremely annoy when using with vscode and copilot, as copilot will execute command in terminal and the wrong conda env will lead to unexpected errors.

2 Answers 2

0

It turns out that whenever a new bash environment is started, the .bashrc or .bash_profile will be executed and force conda env to base.

To fix this, I just keep the current CONDA_PREFIX before running conda init procedure, and recovery the environment after the init procedure is done.

__conda_current_env=$CONDA_PREFIX
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/henry/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/henry/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/henry/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/henry/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
[ -n "$__conda_current_env" ] && conda activate $__conda_current_env
unset __conda_current_env
Sign up to request clarification or add additional context in comments.

Comments

0

If you don't want bash to read the rc files when is started from command line just run

bash --norc

2 Comments

It won't work for bash started by screen, tmux or vscode.
You can tell at least screen and tmux (I don't know vscode) what default shell to run, bash --norc in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.