Sep 20, 2014

Python in OSX

Though OSX comes with a version of python, if another version is required here are some guidelines.


# I have used macport to install the desired version of python (eg., 2.7). This will be installed in the following directory.
cd /Library/Frameworks/Python.framework/Versions/
ls
2.7 Current

# Now move the installed version into the directory /System/Library/Frameworks/Python.framework/Versions/
where all other previously installed python version are.
sudo mv /Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/

# remove the older version of "Current" from /System/Library/Frameworks/Python.framework/Versions/

$ sudo chown -R root:wheel /System/Library/Frameworks/Python.framework/Versions/2.7
$ sudo rm /System/Library/Frameworks/Python.framework/Versions/Current

# create new symbolic link for the newly installed python version (eg, 2.7)
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7 /System/Library/Frameworks/Python.framework/Versions/Current

# remove the old python symbolic link
$ sudo rm /usr/bin/pydoc
$ sudo rm /usr/bin/python
$ sudo rm /usr/bin/pythonw
$ sudo rm /usr/bin/python-config 

# create the new symbolic links so that when you type on the terminal this is the desired python that would execute. This is because python
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pydoc /usr/bin/pydoc
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw /usr/bin/pythonw
$ sudo ln -s /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python-config /usr/bin/python-config

# update the ~/.bash_profile PATH variable to link the current python path
touch ~/.bash_profile
open ~/.bash_profile

PATH=/opt/local/lib:/System/Library/Frameworks/Python.framework/Versions/2.5/bin:/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/texbin:/usr/X11/bin:/usr/X11R6/bin

source ~/.bash_profile


Installed modules via macport successfully get installed into /opt/local/lib/python2.5/site-packages/
But when compiling a python script it doesn't find the path of the modules that are included using import statement such as 

import path
import json
import numpy
import Image

The following error message pops up "ImportError: No module named Image" or "ImportError: No module named simplejson".

Updating the PATH variables to point to the python version or macport installation path did not help. Explicit location of the installed modules needs to be shown to get rid of the error.

Add the path in the python code to show the path of the installed package. For example, to show the installed packages via macport find the path of the location of where the modules get installed:
$port content py25-numpy
It will show the /opt/local/lib/python2.5/site-packages/
Add the path using 
sys.path.append("/opt/local/lib/python2.5/site-packages/")

Similarly, for the downloaded package such as path, add the location of the downloaded copy using sys.path.append() method.
sys.path.append("~
/Desktop/seeing3d_renderer/seeing3d-master/pathpy/
)


One solution could be use the try-catch:
try:
    import json
except ImportError:
    import simplejson as json 


with open(access) as log_file:
Should be replaced with the following for the earlier python version such 2.5.
stream = None
log_file = None
try:
    stream = open(access, 'r')
    log_file = stream.readlines()
except IOError:
    print "Cannot find open the file", access

No comments:

Carlo Cipolla's Laws of Stupidity

    "By creating a graph of Cipolla's two factors, we obtain four groups of people. Helpless people contribute to society but are...