Python Notes for Mac OS X

I started helping my users with using Python on Mac OS X. This post will be a work in progress. I will add to it when I learn more about using Python on Mac OS X. These commands should work on 10.11 or later.

Installing modules

The default method of installing modules is using the command easy_install. Note that this is done outside Python. easy_install is part of the setuptools which is installed by default on Mac OS 10.11 and later. You might need the Xcode and its command line tools. Use this link to download Xcode.

https://developer.apple.com/xcode/downloads/

You can install a module by name. For example, this command install the abf module.

sudo easy_install abf

For a list of available packages, please check PyPI index page. You can also install a module by the module source file.

sudo easy_install https://pypi.python.org/packages/a2/d8/4afbd6ef3b36de04ba86f1df97de96d3b4b66d44cbef7de32bed38d2aedc/abalone-0.1.tar.bz2#md5=c6dc8d6d0d1825151d040f7ccba0a6da

For other use of the command, please check this page on python.org. If you get an error message that you cannot install a package because you do not have permission even though you run the command with sudo, that’s because Mac OS has restrictions on some system directories. You can try to use the –user option and install the module just for you. For example, this command installs the module abf under your home directory.

easy_install --user abf

Also, if you would like to use pip to install packages. You can use easy_install to install pip first, and then you can use pip for module installation.

sudo easy_install pip

Checking the architecture of files

This command is not directly related to Python, but it’s useful when you build a Python module. I got a message saying the module’s architecture is not the same as Python when I built a module. I was looking for a way to check the architecture of the files and I found this command called file. For example, to check the architecture of Python, use

file /usr/bin/python

Here is the output of this command.

/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture i386): Mach-O executable i386
/usr/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64

You can run the file command against the files in the module you built to find out if the architecture of your files matches that of Python.

file _spams_wrap.so
_spams_wrap.so: Mach-O universal binary with 2 architectures
_spams_wrap.so (for architecture i386): Mach-O bundle i386
_spams_wrap.so (for architecture x86_64): Mach-O 64-bit bundle x86_64

In this example, _spams_wrap.so is the file name of an object file in the module I built.

Checking shared libraries

Again, this is a tool not directly related to Python but it’s useful. You can use otool with -L option to show the shared libraries that the object file uses.

otool -L _spams_wrap.so
_spams_wrap.so:
 /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
 /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib (compatibility version 1.0.0, current version 1.0.0)
 /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib (compatibility version 1.0.0, current version 1.0.0)
 /System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.10)
 /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

I used this to make sure the correct shared libraries were used to build the object file.

Alternative ways of running Python

If you want to use a different version of Python, you can consider the following alternatives.

Homebrew

Anaconda

PyDev

 


This post may contain affiliated links. When you click on the link and purchase a product, we receive a small commision to keep us running. Thanks.


Be the first to comment

Leave a Reply