Python3 Make Virtual Environments

Installation

1) Install brew (see snippet)

2) Install python3

1
brew install python3

Verify the python3 installation path. It will be like this but the version may be different.

/usr/local/Cellar/python3/3.6.1/bin/python3

3) Install virtual environment tools with Python3 pip3

1
pip3 install virtualenv virtualenvwrapper

4) Create a directory for environments. For example, ~/pyenvs.

1
mkdir ~/pyenvs

5) Edit your bash startup file, named .bash_profile or other. Add these five lines at the end but modify the directory and python3 path as necessary.

1
2
3
4
5
export WORKON_HOME=~/pyenvs # point to the .envs fold created above
export VIRTUALENVWRAPPER_PYTHON=/usr/local/Cellar/python3/3.6.1/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
source /usr/local/bin/virtualenvwrapper.sh

6) Reload your startup file.

1
. ~/.bash_profile

Using Virtual environments

Create a new environment for a project. The project name is arbitrary. A subdirectory in ~/pyenvs is created, and python3, pip3, and other libraries are copied there from /usr/local to provide a self contained python3 environment.

1
mkvirtualenv hello

Use workon to activate or change environments. The name will prefix the command line.

1
2
3
workon hello
(hello) pip3 install something
(hello) python3 runsomething

Deactivate environment for now. All libraries and settings preserved.

1
(hello) deactivate

Reference