pythongtkshared-librariespygtkjhbuild

How to find shared library used by a python module?


I am debugging a python program based on pygtk and I want to make sure that the program is using the right shared library.

pygtk is a GTK+ wrapper for python. I have already compiled GTK+ using jhbuild tool and I want to make sure that the python script which I am debugging uses the compiled library from jhbuild.

One would import gtk and pygtk like this:

import gtk
import pygtk
print(gtk.__file__)
# /usr/lib/python2.7/site-packages/gtk-2.0/gtk/__init__.pyc
print(pygtk.__file__)
# /usr/lib/python2.7/site-packages/pygtk.pyc

For example I can show a window using gtk:

w = gtk.Window()
w.show()

This will draw a window on the screen using gtk. However I don't know which shared object is be used. I have many versions installed and I need to find the culprit.


Solution

  • I assuming you are using Linux, if the module is dynamically loaded, you could find it out with lsof(8):

    $ python
    >>> import os
    >>> os.getpid()
    29982
    
    $ lsof -p 29982 > before
    

    then back to python, import the module:

    >>> import gtk
    
    $ lsof -p 29982 > after
    $ diff <(awk '{print $NF}' after) <(awk '{print $NF}' before)
    

    yields:

    < /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0.3200.2
    < /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6
    < /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0.2400.30
    < /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.30
    < /usr/lib/python2.7/dist-packages/gtk-2.0/gtk/_gtk.so
    ...