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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With