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

Introduction

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

Goals

  1. I want to switch python version in one machine
  2. I also want to switch the python distribution (anaconda in this case)
  3. I want to resolve conflict between anaconda and pyenv when I execute activate command
    1. When I activate anaconda, I do not execute the command with full path

1 and 2 are pyenv, and 3 can be solved with pyenv-virtualenv.

Environment

  • MacOSX Yosemite
  • homebrew
  • zsh

Setup

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 -)"
6fi
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 available python with pyenv install command. After that, Python version will be changed with 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.

deactivate

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. By using pyenv, you can install various Python distributions.

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

With conda info command, you can check the existing environment information.

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

Next, we build a anaconda virtual environment.

1conda create -n fuga python=3.5 anaconda

With pyenv activate command, you can activate anaconda environment.

1pyenv activate anaconda3-4.1.0/envs/fuga

deactivate is also the same as before.

pyenv deactivate

Conclusions

  • By using pyenv andpyenv-virtualenv, you can switch the virtual environment of python
  • The virtual environment created with venv and the virtual environment created withconda can be activate without conflict usingpyenv
soudegesu avatar
About soudegesu
Software Engineer
comments powered by Disqus