Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distribution independent libpython path

Tags:

python

linux

path

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?

like image 572
Psirus Avatar asked Dec 14 '13 10:12

Psirus


2 Answers

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

like image 80
Yaroslav Halchenko Avatar answered Nov 20 '22 10:11

Yaroslav Halchenko


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.

like image 11
Chiggs Avatar answered Nov 20 '22 12:11

Chiggs