Getting started with Python 3 & virtual environments on OS X Yosemite

Notes on installing Python 3, virtualenv and virtualenvwrapper on OS X Yosemite. (also shared on QUTPy GitHub repository)

Start by installing XCode from the Apple App store. The latest version (6.4) seems to include command line tools (these used to have to be installed separately).

Create a ~/.bash_profile file to set the architecture type:

On OSX one way to install python3 is to install Homebrew and use

If you don’t want to use Homebrew, you can go to https://www.python.org/downloads/ and download the latest python3 from there.

I use virtual environments with Python to manage dependencies, so the next step is to install that (using pip3 makes sure you are using python3):

Virtualenvwrapper makes using virtualenv much easier:

Make directories for your projects and virtualenvs if they don’t already exist (by default virtualenvwrapper will use ~/.virtualenvs)

Use which python3  to find out where your python3 is so you can set it in .bashrc.

Create a ~/.bashrc file to set parameters for virtualenvwrapper:

By setting PIP_REQUIRE_VIRTUALENV=true  it prevents pip from running outside of a virtualenv – this might not suit you if you already have work using the systemwide python environment, but is a way to ‘force’ using virtualenv.

Make your first virtualenv:

This will create the ~/Projects/test1 directory and setup the python libraries for that virtualenv in ~/Virtualenvs/test1. It uses python3 because we set the VIRTUALENVWRAPPER_PYTHON to python3. You can use a command line option to change the python version.

Virtualenvwrapper also activates that virtual environment when you create it. To leave the virtualenv use:

To get back into the virtualenv use:

workon  allows tab completion to see the available environments.

When the virtualenv is active, anything you pip install will be installed just for that virtualenv, not system wide. Once in the virtualenv, the environment is set to the python version you are using, so you can use either pip3 or pip.

See the virtualenvwrapper documentation for other commands to manage your virtual environments.

Try out Jupyter and Pandas

If you want to get started with Jupyter and Pandas you can just install them in your new virtualenv project (this will also install quite a few extra modules that these require):

Start jupyter using:

 

installing latest gensim in Anaconda

I’m using Continuum Analytics Anaconda python because it was the easiest way to get BLAS working for gensim on OSX Mavericks.

I wanted to install the latest gensim (gensim 0.10.2) into Anaconda from the Python Package index (pypi). I followed this tutorial: Tutorial: Basic tutorial for building a Conda package and this Python Packages and Environments with conda for creating a new conda environment for the new gensim.

Create the package

Upload it to BinStar

As recommended in the tutorial, I created an account on BinStar and uploaded the package I’d made to that so I can install it like any other Anaconda package. It also means that other people can use the package from my channel instead of having to build it.

Use the new environment

To activate this environment, use:
source activate gensim0.10.2

To deactivate this environment, use:

source deactivate