To switch virtual environments with conda
conda activate <ENV_NAME>
However, it is troublesome to switch for each project.
You can switch the environment automatically by using direnv. Create .envrc in the corresponding project and write the following.
.envrc
eval "$(conda shell.bash hook)"
conda activate <ENV_NAME>
By doing this, when you move to the corresponding project, .envrc will be executed and the specified virtual environment will be automatically activated. When you exit the project, it will be unset and you can automatically exit from the virtual environment.
By the way,
If you only use conda activate
, you will be asked to execute conda init
first. (I don't think it's happening)
This command should already be running when you set up conda. So it's a bit strange to write it in .envrc obediently.
In the first place, conda init
creates various files and adds them to bash_profile. / <Conda path> /etc/profile.d/conda.sh
is included in this created files. Here is the script for conda activate
.
conda shell.bash hook
will return the contents of that conda.sh. By eval, you can execute conda.sh and use conda activate
.
https://github.com/conda/conda/issues/7980
Recommended Posts