Distribution independent libpython path

余生长醉 提交于 2019-12-22 04:27:10

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/20582270/distribution-independent-libpython-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!