[PYTHON] Automatically switch virtual environment with conda + direnv

Method

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.

reference

https://github.com/conda/conda/issues/7980

Recommended Posts

Automatically switch virtual environment with conda + direnv
Switch virtual environment with jupyter
Virtual environment with Python 3.6
Create a virtual environment with conda in Python
Try using conda virtual environment with Jupyter Notebook
Build python environment with direnv
Build python virtual environment with virtualenv
Create a virtual environment with Python!
Building a virtual environment with Python 3
Operation memo of Conda virtual environment
Create a virtual environment with Python_Mac version
Build a python virtual environment with pyenv
How to easily switch the virtual environment created by Conda on Jupyter
Building a python environment with virtualenv and direnv
Start Django in a virtual environment with Pipenv
Python (Windows 10) Virtual Environment / Package with VS Code
Beginner virtual environment
[Memo] Build a virtual environment with Pyenv + anaconda
Error when entering virtual environment with workon command
Build a virtual environment with pyenv and venv
Work in a virtual environment with Python virtualenv.
Flow of creating a virtual environment with Anaconda
From Python environment construction to virtual environment construction with anaconda
Use anaconda virtual environment with Zsh (problem fix)
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Recommendation of building a portable Python environment with conda
Build a python virtual environment with virtualenv and virtualenvwrapper
Create a virtual environment with Anaconda installed via Pyenv
code-server Online environment (2) Create a virtual network with Boto3
I made a Python3 environment on Ubuntu with direnv.
Build a python virtual environment with virtualenv and virtualenvwrapper
conda memorandum: Building a Python environment with supercomputer ITO
Notes on creating a virtual environment with Anaconda Navigator
Ssh to virtual environment with remote development of vscode
Build a Django environment for Win10 (with virtual space)
python virtual environment Pipenv
virtual environment in python
Python environment with docker-compose
PyTorch introduction (virtual environment)
Reflect the virtual environment created with Miniconda in Jupyter notebook
Move what you installed with pip to the conda environment
Creating an environment that automatically builds with Github Actions (Android)
I want to use a virtual environment with jupyter notebook!
I just built a virtual environment with AWS lambda layer
Building a virtual environment with pyenv-virtualenv/Python (installation, environment settings, packages) Mac environment