14

Under newer Ubuntu/Debian versions, libpython2.7.so is under /usr/lib/i386-linux-gnu/libpython2.7.so or /usr/lib/x86_64-linux-gnu/libpython2.7.so, etc. Earlier, they could be found in /usr/lib/libpython2.7.so, no matter the architecture. I haven't checked for other distributions. How do I find the path of libpython2.7.so with python?

Psirus
  • 1,425
  • 1
  • 14
  • 22
  • Are you looking for `locate libpython` or something else? – janos Dec 14 '13 at 11:09
  • what does your `ldd /usr/local/bin/python` show you ? – Arovit Dec 14 '13 at 11:33
  • `locate libpython` finds quite a lot more than just this single file, and I want to programmatically find the path and supply it to cmake. `ldd /usr/local/bin/python` gives `No such file or directory`, and `ldd /usr/bin/python` gives several library files, but `libpython2.7.so` is not one of them. – Psirus Dec 14 '13 at 15:21
  • there is a package for this: https://pypi.org/project/find-libpython/ – TingQian LI Jul 31 '23 at 07:36

3 Answers3

11

Using pkg-config is not the best option - it will not distinguish between different installations of Python, returning only the system installation. You are better off using the Python executable to discover the location of libpythonX.Y.so.

From inside Python:

   from distutils import sysconfig;
   print sysconfig.get_config_var("LIBDIR")

Or inside a Makefile:

   PYTHON_LIBDIR:=$(shell python -c 'from distutils import sysconfig; print sysconfig.get_config_var("LIBDIR")')

This will discover the location from whatever Python executable is first in $PATH and thus will work if there are multiple Python installations on the system.

Credit to Niall Fitzgerald for pointing this out.

Chiggs
  • 2,824
  • 21
  • 31
4

Here is my solution which seems to work against system wide Debian and CentOS installation, anaconda on Debian, miniconda on OSX, virtualenv on Debian... but fails for system-wide python on OSX:

from distutils import sysconfig;
import os.path as op;
v = sysconfig.get_config_vars();
fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; 
print(list(filter(op.exists, fpaths))[0])

and here it ran on my laptop:

$> for p in python python3 ~/anaconda-4.4.0-3.6/bin/python ~datalad/datalad-master/venvs/dev/bin/python ; do $p -c "from distutils import sysconfig; import os.path as op; v = sysconfig.get_config_vars(); fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; print(list(filter(op.exists, fpaths))[0])"; done                                                                                                                               
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so                                                      
/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
/home/yoh/anaconda-4.4.0-3.6/lib/libpython3.6m.so
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so

P.S. I had no clue that it is such a problem... bad bad bad Python

0

I'm assuming you're looking to link against this file. Python is usually installed with pkgconfig info to help compile against it. Specifically for the .so file, you should use pkg-config --libs python-2.7. From Python:

import subprocess
subprocess.check_output(["pkg-config", "--libs", "python-2.7"])

If the only flag shown is -lpython2.7, you might want to consider reading /etc/ld.so.conf to see default locations in which the linker looks for its libraries.

lutzky
  • 598
  • 5
  • 12