Friday, 26 July 2013

Installing Python modules without root privileges

It seems I'm doomed to work on centrally-administered Linux systems without root privileges. I've already had to write about similar problems before. I'm on a much more up-to-date distribution (link OpenSUSE) these days, so LaTeX and out-of-date software are generally no longer problems, but I often find myself wanting to install one or another nifty Python package for my work. Luckily, it turns out someone out there is thinking of people like me. The standard distribution mechanism of Python packages includes a method to install a package in your user space. The official description is here but here are the basics.

All the packages in the Python Package Index are installed with a setup script, always setup.py. Usually, you could install these packages by downloading them, extracting them, then changing to the new directory and typing

sudo python setup.py install

These now include the option to install to your user space by instead entering

python setup.py install --user

By default, this creates a root-like tree under ~/.local/. With that done, you're basically good to go! Path variables seem to be updated and you can import them in Python, IPython or scripts. It's always handy to test by entering

python -c "import python-package"

at the command line and watching for errors.

All that said, there's a known conflict with the default settings in OpenSUSE and RedHat. (I'm not sure if it applies to other RPM-based distributions.) This can be fixed by adding the argument --prefix== to our command. So the full command becomes

python setup.py install --user --prefix==

That's it! As a closing note, this method also carries the handy benefit of following your around as you login to different computers, presuming they have the same architectures and Python packages. This happens to be pretty handy when either offloading work onto other computers in a network, or using a cluster. Just be sure to set up the appropriate environment variables if they aren't automatically loaded.

3 comments: