3 types of workarounds for activate collision problem when pyenv and anaconda coexist

This is a supplement to python environment construction definitive edition.

20180312 postscript

It seems that conda activate was adopted in conda 4.4.0 released on 2017-12-20. conda/CHANGELOG.md It seems that source activate is not obsolete at the moment, but conda activate is recommended.

Since docker has become familiar recently (I do not feel stress even in the windows environment through proxy), the need to insert pyenv has decreased, and I have also moved to docker-anaconda, but for reference.

Comment. Thank you very much.

What is the activate collision problem between pyenv and anaconda?

On linux and Mac you can install anaconda via pyenv. Furthermore, I think there are cases where you want to switch between multiple development environments with anaconda. The environment in anaconda can be switched as source activate <environment name>, but under pyenv, the following error will occur and the entire shell will be dropped.

$ source activate
#>>> pyenv: -bash: command not found

I'm not sure why, but pyenv uses a shim script to track the path, so something strange may be happening.

If anaconda has adopted conda activate ... orz

solution

1. Do not use pyenv.

Definitive Edition basically introduces this method. This method uses pyenv only as an anaconda installer. After installing anaconda via pyenv, write the installation path of anaconda to path and let pyenv be ignored.

$ echo 'export PATH="$PYENV_ROOT/versions/anaconda3-2.5.0/bin/:$PATH"' >> ~/.bashrc

--Advantage: The fewest settings and less to remember. --Disadvantage: pyenv local cannot be used.

With this method, you will not be aware of pyenv at all after setting the path.

Use source activate for all environment switching.

2. Run activate with the full path.

In 1., pyenv local cannot be used, but I think this is fatal for some people. Especially for web development people who want to change the package dependency for each PJ, I think it is more efficient to switch the environment for each working folder with pyenv local. It's a simple solution, but if you specify activate with the full path, you can execute it with conda activate.

$ source $PYENV_ROOT/versions/anaconda3-2.5.0/bin/activate <Environment name>

--Advantage: Simple --Disadvantage 1: Many types to activate

The disadvantage can be solved by setting alias.

$ echo 'alias activate="source $PYENV_ROOT/versions/anaconda3-2.5.0/bin/activate"' >> ~/.bashrc
$ source ~/.bashrc

** That? This is good ?? **

You can switch the environment for each working folder by using pyenv local.

$ conda create -n py2 python=2.7
$ mkdir py2
$ cd py2
$ pyenv local anaconda3-2.5.0/envs/py2
$ python
#>>>Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec  6 2015, 18:08:32)
#>>>[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

To switch the environment, use ʻactivate and pyenv local` together.

3. Insert pyenv-virtualenv

It is the method described in Addition of definitive edition. Put pyenv-virtualenv, a plugin for pyenv, and use pyenv activate.

Installation

$ git clone git://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
source ~/.bashrc

Easy to use pyenv-virtualenv

$ conda create -n py2 python=2.7
$ mkdir py2
$ cd py2
$ pyenv activate anaconda3-2.5.0/envs/py2
$ python
#>>>Python 2.7.11 |Continuum Analytics, Inc.| (default, Dec  6 2015, 18:08:32)
#>>>[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

$ pyenv deactivate

Note that if you use source activate by mistake, the shell will fall.

Benefits: Probably the correct usage. Disadvantages: Putting pyenv-virtualenv just for this ...

To switch the environment, use pyenv activate and pyenv local together.

Note

Both methods use conda for virtual environment management. (Do not use virtualenv.) I think you like which one of 1-3 to choose. I used to be one sect, but recently I have become two sect.

Recommended Posts

3 types of workarounds for activate collision problem when pyenv and anaconda coexist
Incorrect answer when using numpy.prod () for B problem of ABC169
Mechanism of pyenv and virtualenv
Coexistence of pyenv and autojump
Pitfalls and workarounds for pandas.DataFrame.to_sql
[Python] Error and solution memo when using venv with pyenv + anaconda