Valid Python Scripts may fail in OS X ‘El Capitan’

Python scripts can be run on OS X using eAPI to access Arista Switches or vEOS instances. Python Scripts can also be run on OS X to automate the installation of vEOS: (https://github.com/arista-eosplus/packer-veos).

After installing or upgrading to OSX ‘El Capitan’ (OS X version 10.11.5) Python Scripts that previously worked fine under earlier versions of OS X or work on other operating systems may fail. This is evident when making a Python Script executable after issuing the chmod +x command.  This is due to operating system changes Apple has introduced in ‘El Capitan’. Although, this problem may affect eAPI and Packer scripts, it is a systemic issue with OS X and not directly related to eAPI or EOS/vEOS.

The script will yield various traceback and import errors similar to the below:

mymac$ vi showv.py
mymac$ chmod +x showv.py
mymac$ ./showv.py
Traceback (most recent call last):
File "./showv.py", line 3, in <module>
from jsonrpclib import Server
ImportError: No module named jsonrpclib

There are two workarounds to this. The first is to run the script using the Python interpreter directly:

mymac$ python ./showv.py
[{u'architecture': u'i386',
u'bootupTimestamp': 1446690373.22,
u'hardwareRevision': u'',
u'internalBuildId': u'0714b2b3-980f-431b-a107-73e403b001dc',
u'internalVersion': u'4.15.6M-3135964.4156M',
u'memFree': 120224,
u'memTotal': 1897592,
u'modelName': u'vEOS',
u'serialNumber': u'',
u'systemMacAddress': u'00:11:22:33:44:55',
u'version': u'4.15.6M'}]

The second workaround is to change the program loader (a.k.a ‘she-bang’) on the first line of the script to the script to  /usr/bin/env python instead of using /usr/bin/python:

mymac$ chmod +x ./showv.py
mymac$ cat showv.py

#!/usr/bin/env python
from jsonrpclib import Server
from pprint import pprint
switch = Server("http://admin:admin@172.16.130.98/command-api")
response = switch.runCmds(1,["show version"])
pprint (response)

mymac$ ./showv.py
[{u'architecture': u'i386',
u'bootupTimestamp': 1446690373.22,
u'hardwareRevision': u'',
u'internalBuildId': u'0714b2b3-980f-431b-a107-73e403b001dc',
u'internalVersion': u'4.15.6M-3135964.4156M',
u'memFree': 120272,
u'memTotal': 1897592,
u'modelName': u'vEOS',
u'serialNumber': u'',
u'systemMacAddress': u'00:11:22:33:44:55',
u'version': u'4.15.6M'}]

Using /usr/bin/env will execute the code while importing the user’s environmental shell variables, including the $PATH variable.

This note is not applicable to Python code running directly on EOS. However, using /usr/bin/env python as the program loader is fully supported on EOS/vEOS.