Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 825
mehdix Avatar asked Nov 16 '17 17:11

mehdix


2 Answers

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
...
like image 60
georgexsh Avatar answered Oct 19 '22 05:10

georgexsh


Whenever I want to know who uses what, strace is my best friend. So strace -ff -olog -eopen python yourscript.py gives you the list of all the files opened in this process and its children. It stores the files in log.* files, the extension being the pid of the process. Then you just grep -i gtk log.* | grep py and look at the results.

However, I wonder why you're using pygtk (outdated and deprecated, last release in 2011) that only works with GTK+ 2. That's useful only if you're debugging a very old program with a very old jhbuild moduleset that still uses GTK+ 2.

If you can, you should strive to use pygobject and GTK+ 3.

https://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html

like image 20
liberforce Avatar answered Oct 19 '22 07:10

liberforce