Python on Mac OS X

Posted on Thu 09 June 2016 in macosx

Python on Mac OS X

Using Python on Mac OS X is a little bit tricky since there are couple of ways to install and use it.

There are basically the following options:

  1. Use the already installed Python version provided by Mac OS X
  2. Install the official Python packages from the Python webpage
  3. Install Anaconda
  4. Install Python using homebrew

ad 0) Each of the lastest Mac OS X Versions brings already a version of Python that is used for its internal scripts and tools. However, at some point the provided version may be outdated and there are some modifications by Apple that can cause strange side effects. For development purposes this option is strongly discouraged.

ad 1) Another option is to simply install the Python package from the official Python webpage. An installer is provided that sets everything up. However, using the installer you need to keep track of the Python installation and on Mac OS X updates things may get broken -- so not the best option for Python development.

ad 2) Anaconda is an open data science platform powered by Python that includes already a lot of preinstalled packages. It installs nicely in a subfolder of the home directory and thus, it is separated from the system's Python installation. If you do not want to take care of installing Python packages and figuring out dependencies this may be the right option for your. However, if you only need a subset of those packages and want to know what is really installed, Anaconda feels a little bit bloated. Besides, though Anaconda provides its own package manager, sometimes adding package will become very tricky.

ad 3) Homebrew is a package manager that allows the installation of software packages on Mac OS X. An up-to-date Python version can be installed using homebrew. pip is already included in homebrew's Python so that additional Python packages can be easily installed. Everything is cleanly installed apart from the system directories in dedicated homebrew directory. Moreover, as package manager homebrew can take care of package dependencies and also install Python modules via pip that are linked to libraries that must be compiled on the system. Additionally, the updating of Python and its related packages is very easy because the update process is managed by homebrew. This is the most flexible solution and thus, well-suited for Python development.

Since homebrew is my preferred choice, more details in the following section.

Installing Python using Homebrew

If you do not have installed homebrew yet, checkout the Homebrew webpage how to install it.

Afterwards, the current Python version can be installed as follows:

brew install python

Additional Python Packages

Virtualenv

In general, it is recommended to make use of isolated Python environments e.g. using virtualenv. This has the advantage that different Python module versions for different Python projects can live next to each other on the same system.

Homebrew

However, some larger packages do not really install nicely in these virtual environments so they can alternatively be installed globally.

For example, the basic packages scientific computing can be installed globally via the brew command:

Install numpy and scipy, two common packages for scientific computing.

brew install numpy
brew install scipy

The correct installation can be checked as follows:

python -c 'import numpy ; numpy.test();'
python -c 'import scipy ; scipy.test();'

For the graphic output (e.g. as png) cairo can be installed:

brew install cairo

For the plotting of graphs matplotlib is recommended:

brew install matplotlib --with-cairo
Pip

A couple of packages are not available via homebrew, hence must be installed via pip:

For the analysis of data in a structured manner pandas can be installed as well as the machine learning module scikit-learn:

pip install pandas
pip install scikit-learn

Also the interactive python can be installed via pip

pip install ipython[all]