Setting to avoid python and anaconda conflict using pyenv and pyenv-virtualenv

Setting to avoid python and anaconda conflict using pyenv and pyenv-virtualenv
Page content


I have been using pyenv only to switch versions of python in local environments. However, recently I use anaconda more often. Since python and anaconda conflict when executing activate command, I decided to use pyenv-virtualenv.


  1. Switch python version in one machine
  2. Switch the python distribution (anaconda in this case)
  3. Resolve conflict between anaconda and pyenv when I execute activate command
    1. Activate anaconda without executing the command with full path

Both 1 and 2 above are able to solved with pyenv and 3 with pyenv-virtualent.


  • MacOSX Yosemite
  • homebrew
  • zsh


Install modules for switching python virtual environments

1# install pyenv
2brew install pyenv
3# install pyenv-virtualenv
4brew install pyenv-virtualenv

Edit configuration file at terminal startup

  • Add the following settings to .zshrc
1# setting for pyenv
2export PYENV_ROOT="${HOME}/.pyenv"
3if [ -d "${PYENV_ROOT}" ]; then
4    export PATH=${PYENV_ROOT}/bin:${PYENV_ROOT}/shims:${PATH}
5    eval "$(pyenv init -)"
7# setting for pyenv-virtualenv
8if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
  • Reboot terminal(you can also use source command)

Create a Python virtual environment(for Python)

Create directory

In this time, I switch to the arbitrary python version under hoge directory.

Print default python version.

1cd ~
2mkdir hoge
3cd hoge
4python -V
5> Python 2.7.6

Specify python version

Install any available version of Python with pyenv install command and also change pyenv local command.

1pyenv install 3.6.0
2pyenv local 3.6.0
3python -V
4> Python 3.6.0

Create virtual environment

Create python virtual environment in current directory with venv module. Activate the virtual environment by loading activate shell.

1python3 -m venv .
2source bin/activate

And use deactivate command to deactivate virtual environment.


Create a Python virtual environment(for anaconda)

Create directory

Create a fuga directory for anaconda environment. Also make sure the python version is the default.

1cd ~
2mkdir fuga
3cd fuga
4python -V
5Python 2.7.6

Specify anaconda version

Install anaconda with pyenv install command. It is available to install various python distributions by using pyenv.

1pyenv install anaconda3-4.1.0
2pyenv local anaconda3-4.1.0
3python -V
4> Python 3.5.1 :: Anaconda 4.1.0 (x86_64)

Create virtual environment

And the existing environment can check from conda info command.

1conda info -e
2> root                  *  /Users/XXXXXXXXX/.pyenv/versions/anaconda3-4.1.0

For the next, I build a anaconda virtual environment.

1conda create -n fuga python=3.5 anaconda

Now activate anaconda environment with pyenv activate command.

1pyenv activate anaconda3-4.1.0/envs/fuga

And use pyenv deactivate command to deactivate virtual environment.

pyenv deactivate


It is available to

  • Switch the virtual environment of python by using pyenv and pyenv-virtualenv
  • Activate multiple virtual environments, created with either vend or conda, without conflicting