wryFi: word

facetious fidelity

since 2008

Most Recent

Categories

Tags

Links

Arch Linux python paths

Wed Nov 19 2014

in

programming -nix
python debian arch

isolation, debian-style

I've been a Debian user for many years, and Debian does some smart things with its python packaging, facilitating isolation of python modules installed by the OS package manager from python modules installed by setuptools/pip. This is not only convenient, but also follows the Filesystem Hierarchy Standard, which states that "locally installed software must be placed within /usr/local rather than /usr unless it is being installed to replace or upgrade software in /usr."

I've recently started running Arch Linux on my laptop instead of Ubuntu, and the "Arch Way" is to avoid patching upstream at all cost. I just ran into some issues with python and a polluted mess of both pacman-installed and pip-installed packages in the singular site-packages directory under /usr.

So I partially recreated the Debian-style isolation in Arch by leveraging the sitecustomize module, which is reserved specifically for this kind of thing. I created /usr/lib/python2.7/site-packages/sitecustomize.py and /usr/lib/python3.4/site-packages/sitecustomize.py with the following content:

import os
import site
import sys
PYTHON = 'python' + sys.version[:3]
SITE = os.path.join('/usr', 'lib', PYTHON, 'site-packages')
LOCAL = os.path.join('/usr', 'local', 'lib', PYTHON, 'site-packages')
def get_site_index():
index = -1
for path in sys.path:
index += 1
if path == SITE:
return index
return None
index = get_site_index()
site.addsitedir(LOCAL)
if index:
sys.path.pop(index)
sys.path.insert((index + 1), SITE)

This inserts /usr/local/lib/pythonX.y/site-packages into the python path just before /usr/lib/pythonX.y/site-packages. (An alternative option would be to use a .pth file to refer to the /usr/local path, but this would place the local packages after the pacman packages, and I want my local packages to take precedence.)

Now I can use pip to install modules into /usr/local by passing it --install-option="--prefix=/usr/local". Of course, I'm not going to remember to always use that flag, so I created /root/.pip/pip.conf as follows:

[install]
install-option=--prefix=/usr/local

Now I can pip install away, and my pip-installed modules will end up in /usr/local, safe from the clutches of pacman. I just have to remember to copy sitecustomize.py into site-packages for any new versions of python.

Personally, I think the usability (not to mention FHS compliance) that Debian-style isolation provides should trump the philosophy of the "Arch Way," but I'm not even going to try to convince the Arch maintainers of that. C'est la vie.