I'm on a SUSE machine where the default Python site-packages location is /usr/lib64/python2.6/site-packages. Some packages automatically install themselves in /usr/lib/python2.6/site-packages instead. How do I configure Python so that it also looks in /usr/lib64/python2.6/site-packages?
Configure Python to include another directory when looking for packages
python;suse
(Please correct errors and omissions as necessary. Thanks.)First, a question and a comment. I don't use SUSE, so take this with a pinch of salt. Are the packages that install in /usr/lib/python2.6/site-packages official packages? If so, SUSE is broken, so that is not likely. If they are not official packages, you could either ask the packagers to use the standard paths, or, alternatively, you couldsubmit a wishlist bug to SUSE asking them to support this additional path. This will save you and other people additional headaches.For the moment, you have the following possibilities, in order of decreasing scope:Change the module search path for all users (method 1)Change the module search path in thePython installation.The default module search path ishardwired into the binary. Add-onpaths can be configured at runtime,for example in the site.py file.For example, Debian uses/usr/lib/python2.6/site.py (for thedefault python 2.6 installation) todo its site-specific configuration.At the top of the file is writtenAppend module search paths for third-party packages to `sys.path`.***************************************************************** This module is automatically imported during initialization. *****************************************************************[...]This will append site-specific paths to the module search path.The Debian patchdebian/patches/site-locations.diffsaysFor Debian and derivatives, this  sys.path is augmented with directories  for packages distributed within the  distribution. Local addons go into  /usr/local/lib/python/dist-packages,  Debian addons install into  /usr/{lib,share}/python/dist-packages.  /usr/lib/python/site-packages  is not used.The patch in question is                sitedirs.append(os.path.join(prefix,Lib, site-packages))             elif os.sep == '/':    +            sitedirs.append(os.path.join(prefix,local/lib,    +                                        python + sys.version[:3],    +                                        dist-packages))                 sitedirs.append(os.path.join(prefix,lib,                                             python + sys.version[:3],    -                                        site-packages))    -            sitedirs.append(os.path.join(prefix,lib, site-python))    +                                        dist-packages))    +            sitedirs.append(os.path.join(prefix,lib, dist-python))             else:                 sitedirs.append(prefix)                 sitedirs.append(os.path.join(prefix,lib, site-packages))So you could modify the site.path inyour system package to produce amodified module search path. Youprobably don't want to this, though.For one thing, you will have tomerge this in on every update ofyour distribution's python package.Change the module search path forall users (method 2)Add a file of the form something.pthto a directory that is already inthe search path, which contains apath, either relative or absolute.Eg./usr/local/lib/python2.6/dist-packages$ cat foo.pth /home/faheem/dummypythonpathIn another terminal do>>> import sys>>> sys.path    [...,'/home/faheem/dummypythonpath',...,]Change the module search path forall users (method 3)The environment variable PYTHONPATHis normally used to append to thesystem path at user level. You canput it in a file which will besourced by all users. Eg. in Debianwe have /etc/bash.bashrc, which saysat the top# System-wide .bashrc file for interactive bash(1) shells.# To enable the settings / commands in this file for login shells as well,# this file has to be sourced in /etc/profile.So you could add or PYTHONPATHthere. You probably want it to besourced for both login andinteractive shells, so you'll wantto check on that. Unfortunately,distributions are often flaky aboutenabling this.The paths in PYTHONPATH are added to the default list of search paths in the system (which can be obtained forexample by sys.path - see below). Allowing for thepossibility that PYTHONPATH is set already,just add desired additional directories to it, eg.export PYTHONPATH=$PYTHONPATH:/home/username/lib/python2.6/dist-packagesIf you source the PYTHONPATH variable, and then check sys.path again, you willsee the paths have been added. Note that the position in which the paths inPYTHONPATH are added to the pre-existing paths does not seem to be prescribed bythe implementation.Change the module search path per user.The usual way is to changePYTHONPATH in the user's bashrc,namely ~/.bashrc. Again, check thatit is sourced for both login andinteractive shells.Change the module search path on a per script basis. Thisis done by appending to sys.path,namelyimport syssys.path.append(some_additional_path)This will only work for the scriptthat is importing this. This isnormally used, as far as I know, forcasual use, when importing modulesin nonstandard locations, like fromsomewhere in a home directory.See also Greg Ward on Modifying Python's Search Path. This has a good discussion of the available alternatives.