Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python interpreters on OS X Yosemite - which one to use?

I recently switched from Windows to Mac, and after installing PyCharm I had to specify an interpreter. In the drop down I could choose between 3 interpreters:

  1. Macintosh HD ▸ usr ▸ local ▸ Cellar ▸ python ▸ 2.7.9 ▸ Frameworks ▸ Python.framework ▸ Versions ▸ 2.7 ▸ bin
  2. Macintosh HD ▸ System ▸ Library ▸ Frameworks ▸ Python.framework ▸ Versions ▸ 2.7 ▸ bin
  3. Macintosh HD ▸ System ▸ Library ▸ Frameworks ▸ Python.framework ▸ Versions ▸ 2.6 ▸ bin

(Actually I can se that there is also version 2.5 and 2.3 in that last folder, but these where not shown in PyCharm).

However, if I type python in the Terminal and then type

import sys
print sys.executable

i get:

  1. /usr/local/opt/python/bin/python2.7

To make it even more confusing, when I type the same thing in IPython Notebook (run from the Terminal using ipython notebook) I get:

  1. /usr/bin/python

Questions:

  • If I want to use Python 2.7 (I have 3 to choose between), which one should I use?
  • How do I navigate between these interpreters (if I want to pip install on different ones)?
like image 842
elgehelge Avatar asked Dec 15 '22 17:12

elgehelge


1 Answers

You really only have two Python 2.7 installations, as well as a 2.6 Python version you can mostly ignore:

  • /usr/local/Cellar/ is user-installed (through Homebrew). It'll be linked into the /usr/local/opt directory structure:

    $ /usr/local/bin/python -c "import sys; print sys.prefix"
    /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7
    

    with /usr/local/opt/python being a symlink to the Cellar directory:

    $ ls -la /usr/local/opt/python
    lrwxr-xr-x  1 mj  admin  22 Jan  5 18:36 /usr/local/opt/python -> ../Cellar/python/2.7.9
    

    This structure allows you to easily enable and disable Python in the /usr/local tree without having to fully reinstall the homebrew Python if you need again at a later time, as well as swap between specific versions.

  • 2.7 is the current version, used by OS X software itself (and has a few extra libraries installed that may clash as they come before site-packages in the Python package path). It is installed in /System/Library/Frameworks, but /usr/bin/python and /usr/bin/python2.7 are the same Python installation:

    $ /usr/bin/python -c "import sys; print sys.prefix"
    /System/Library/Frameworks/Python.framework/Versions/2.7
    
  • 2.6 and the other folders are there for legacy software that required a specific version on previous versions of OS X; Apple appears to have decided that anything requiring 2.3 or 2.5 can run fine with 2.6 (and they'd be right, for the most part).

For new software development, use either the 2.7 system-installed version or the Homebrew version; the latter is easier to upgrade if you need fixes in new 2.7.x releases. Always use a virtualenv to install additional packages, however, especially if you use the OS X 2.7 version. Also see Creating Virtual Environment in the PyCharm documentation.

PyCharm otherwise lets you configure what interpreter to use per project, see Project Interpreter.

IPython is a Python application, it is built on top of Python. As such it is tied to a Python interpreter. Which one depends on how it was installed. Yours is tied to the OS X Python 2.7 interpreter, but you can also install it for the brew version (using the pip tool if correctly installed for that Python installation).

like image 113
Martijn Pieters Avatar answered Dec 29 '22 10:12

Martijn Pieters