Linking to a library in your own directory

John Hunter jdhunter at ace.bsd.uchicago.edu
Fri May 16 11:13:47 EDT 2003


>>>>> "Jay" == Jay O'Connor <joconnor at nets.com> writes:

    Jay> I'm trying to import the postgresql library into a python
    Jay> script on a machine on which postgress is installed.  I can't
    Jay> reall install postgres on this machine, but I want to use
    Jay> this script to connect to a postgres db on a different
    Jay> machine.

    Jay> The problem I'm having is that import the pg module wants to
    Jay> import libpq.so, which it cannot find (not being installed) .
    Jay> I an put libpq.so.3 in my own directory, but I'm not sure how
    Jay> I can get other programs to see it so the link it in.

If you are on a linux/unix like machine, the relevant environment
variables are LD_LIBRARY_PATH and PYTHONPATH.  The former is the path
that the dynamic linker will use to find its libraries, and the latter
is the path python uses to find its modules.  Something like

# bash and friends
> export LD_LIBRARY_PATH=/your/special/path:/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib

# csh and friends
> setenv LD_LIBRARY_PATH /your/special/path:/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib


If you are running in the dir that contains the shared lib, you can
just add '.' to your LD_LIBRARY_PATH.

Also, simply copying over the library may not be enough, if it depends
on other libraries.  You can do

> ldd libpq.so.3

to see what libraries it depends on.  For example, on my system

mother:~/python/projects/matplotlib> ldd /usr/lib/libpq.so.2.1
        libcrypt.so.1 => /lib/libcrypt.so.1 (0x40013000)
        libc.so.6 => /lib/i686/libc.so.6 (0x40041000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)


You *may* also need to create a symlink from libpq.so->libpq.so.3, but
about this, I am not sure.  So, if the above doesn't work, try
> ln -s libpq.so.3 libpq.so

JDH





More information about the Python-list mailing list