Catalogue
Building Python 2 and Python 3 Virtual Environments on Mac OS X

Building Python 2 and Python 3 Virtual Environments on Mac OS X

🌐 日本語で読む

Background

By default, Mac OS X ships with Python 2.

Since Python 2.7 is only supported until 2020,
I decided I should get familiar with Python 3
and set up a Python 3 environment.

That said,
there were cases where things like dlib wouldn’t configure properly unless I used Python 2
(although this may also have been my own mistake),
so I decided to keep both around and build virtual environments for each.

Environment

1
2
3
4
5
$ sw_vers

ProductName: Mac OS X
ProductVersion: 10.11.5
BuildVersion: 15F34

Installing Homebrew

Please refer to the official site below.

Homebrew Ja

Installing Python 2 and 3

As of 2016/07/28, python = 2.7.10, python3 = 3.4.3

1
$ brew install python python3 pyenv

Configuration File

Add the following to .bashrc or .zshrc.

Here we’ll assume .bashrc.

1
$ vi ~/.bashrc
1
2
3
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
  • Reload the configuration
1
$ source ~/.bashrc

Installing virtualenv

1
sudo easy_install virtualenv

Building the Virtual Environments

  • Building a virtual environment with Python 2
1
2
3
$ which python

/usr/local/bin/python2.7
1
$ virtualenv -p /usr/local/bin/python2.7 ~/py2env
  • Building a virtual environment with Python 3
1
2
3
$ which python3

/usr/local/bin/python3
1
$ virtualenv -p /usr/local/bin/python3 ~/py3env

Switching Between Virtual Environments

  • Switch to the Python 2.7 virtual environment
1
$ source ~/py2env/bin/active
  • Switch to the Python 3.4 virtual environment
1
$ source ~/py3env/bin/active

A bit late to the party, but that’s my memo-style summary.

kenzo0107

kenzo0107