Installing multiple python versions on Mac OS X

Why would you need multiple versions of python anyway? My Macbook came installed with python 2.7 so that should be enough. And that is true for most cases. However, there are some packages and features that work only with Python3.5 or so, e.g. Blender, which comes with it’s own python console. I wanted to setup Blender’s python to work with numpy,  cvxopt and other optimization packages for a research project. However, there is no straight forward way to add these just to Blender.

First, we need to install the same python version as Blender uses. Using mac ports:

sudo port install python35
sudo port select --set python3 python35

And we do ‘port select’ to set the alias python3. Then we need to install the relevant pip version for managing and installing python packages:

sudo port install py35-pip
sudo port select --set pip pip35

We setup pip to work with pip35 instead of our previous pip, which was associated with python 2.7. To know which pip is running:

pip --version

If it says something like …(python 35), we are ready to move to the next step.

sudo pip install numpy
sudo pip install cvxopt

In the terminal, add a soft link to connect between the folder where pip installs the above and Blender’s python library.

ln -s /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/* <BlenderFolder>/blender.app/Contents/Resources/2.77/python/lib

Almost done. One final step is to switch back pip to use your previous pip configuration, i.e. the pip version that used python 2.7.  This step is so you don’t confuse between these versions, because each pip version installs python packages in a different folder.

sudo port select --set pip none

This article was adapted from:
www.janosgyerik.com/working-with-different-versions-of-python-on-osx-using-macports/
www.lowindata.com/2013/installing-scientific-python-on-mac-os-x/
stackoverflow.com/questions/14117945/too-many-different-python-versions-on-my-system-and-causing-problems

Advertisements

Python tips and tricks

globals() to check all global variables
locals() to check local variables
<classInstance>.__dict__ to view class fields
Be very carefully when referencing and changing object. A good way to check if the object is the same one or not is via the id() method.
Awesome blog about python

Adding 3d party Python libraries

Have you ever had a situation where you tried to install a python library with ‘pip install’ and it didn’t work out of the box? Well that happenned to me today. Woohoo.

I was try to get PySide to work with:

sudo pip install  -U PySide

Of course it didn’t. I got a error message about qtmake not being found, even though I installed it before with mac ports. So instead I opted to install PySide with mac ports, specifically:

sudo port install py27-pyside

The installation finished successfully, but after that PySide still didn’t work well. This was because the mac ports installation library was not added in the Python Path.

Default Pip installation library:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

Default Macports installation library:

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

So the most basic way to get a python script that uses PySide is to add the following in the top of the file:

import sys
sys.path.append(‘/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages’)

Other related links: