Which Python

Matthew Brett edited this page Aug 7, 2014 · 7 revisions

Why you should not use system Python for OSX

OSX comes with a version of Python installed at /usr/bin/python. We'll call this "system Python".

There are a variety of guides for working with Python on OSX (see list below), and all of them suggest installing another Python:

So - why do these guides suggest using a Python other than the one that comes installed on OSX by default?

There are various problems with system Python:

  • It can be old, especially on older versions of OSX. For example, OSX 10.6 (Snow Leopard) has Python 2.6 as system Python, where the current standard Python 2 version is 2.7. The alternative Pythons listed above are up to date with features and security fixes.
  • Updating OSX has the potential to change or break any updates you have made to the Python installation, including installation of Python packages.
  • System Python has an unusual setup for some Python packages that makes it much more difficult to upgrade these packages

System Python and extra Python packages

Apple has kindly installed some optional Python packages into its Python, but did that in a way that is not standard and therefore confusing to work with.

As y'all know, Python searches for packages using the module search path.

Here is the module search path for system Python:

$ /usr/bin/python
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Users/mb312/Library/Python/2.7/lib/python/site-packages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages

The interesting entry is /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python because this entry is specific to system Python. Call this path "Extras/lib". On a standard OSX 10.9 machine this directory contains some Python packages including the numerical packages numpy, scipy and matplotlib and other useful web packages including twisted.

If you, the user, install new packages in system Python, the packages will get written to /Library/Python/2.7/site-packages. As you can see from the path listing above, your newly installed package will therefore be lower in the Python module path than the packages in Extras/lib. That means that if you try to install a new version of one of the packages in Extras/lib into system Python, system Python will ignore the version you installed and continue to use the version in Extras/lib.

Apple presumably chose this setup because they use Python for system tasks, and they want to be able to depend on working versions of the packages installed in Extras/lib. This is another reason to prefer another Python for your daily Python work.