Setting up Python environment with Anaconda and Homebrew

YH
2 min readApr 16, 2018

--

Recently when I tried to setup Python environment, I encountered errors since Python environments set up by Anaconda and Homebrew overlapped. So I would like to walk you though how to set up Python using both approaches and how to avoid overlapping issues.

  1. Setting up Python env with Anaconda is pretty straightforward. In the following example, your Python version for the new virtual environment is 3.6. I really like Anaconda because it includes most popular scientific computing packages you need for machine learning and deep learning and is very easy to work with. Plus, Anaconda also provides conda-forge and conda pip for you to install more packages that are not included in their main channel.
# install python 3.6
conda create -n <your_virtual_env> python=3.6 anaconda
# update python
conda update python
# install package
conda install tensorflow
# enable the virtual env
source activate
# disable the virtual env
deactivate

2. If you want a more controlled Python environment, you can use Homebrew combined with pip — a package manager for Python packages. My trigger to use Homebrew was because one of Python packege — pyfasttext is not available from Anaconda. Here are my steps.

# python installed by brew is python3
brew install python
# need to use pip3 since we are using python3
pip3 install --upgrade pip setuptools wheel
# install virtualenv & virtualenvwrapper
pip3 install virtualenv virtualenvwrapper

It is very important to set up environment variables correctly. Since $PATH is set as “/Users/username/anaconda/bin:$PATH” in my ~/.bash_profile, when I try to set up virtual environment by Python from Homebrew using virtualenv command, Python always calls Anaconda libraries. So I decided to use virtualenvwrapper from Homebrew Python library to manage the virtual environments directly. This requires me to set up the following environment variables.

# put all virtual environments in a centralized location
export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
# set environment variables
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
# install virtalenvwrapper
source /usr/local/bin/virtualenvwrapper.sh

Once virtualenvwrapper is installed properly, you can create a virtual environment or multiple virtual environments and switch among them easily.

# create virtual env
mkvirtualenv <your_virtualenv_name>
# deactivate
deactivate
# activate
workon <your_virtualenv_name>

The lesson I learned from setting up different Python environments is that I need to set my environment variables properly and separate each environment completely. Constantly checking $PATH and “which python” is helpful. Finally these are very useful references I used and highly recommend.

--

--