编程语言
首页 > 编程语言> > python – 自Mac OS 10.11 El Capitan以及系统完整性保护以来的DYLD_LIBRARY_PATH技巧的替代方案

python – 自Mac OS 10.11 El Capitan以及系统完整性保护以来的DYLD_LIBRARY_PATH技巧的替代方案

作者:互联网

这是我有的:

> Mac OS 10.11 El Capitan
> python 2.7.12,从/Library/Frameworks/Python.framework/下的python.org安装
> PyCharm 2016.2.3
> vtk 7.1.0

这是我做的:

>在本地构建一个python模块.在我的例子中,这是vtk.有关摘要,请参阅我配置vtk的CMake调用.

cmake -G Ninja .. -DCMAKE_BUILD_TYPE=Release -DVTK_WRAP_PYTHON=ON -DBUILD_EXAMPLES=OFF -DBUILD_SHARED_LIBS=ON -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX="/opt/dev/versions/vtk/vtk-7.1.0-shared" -DPYTHON_INCLUDE_DIR="/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/" -DPYTHON_LIBRARY="/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib"

>将python包安装在python可以找到它的位置.在我的例子中,这是/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
请注意,我需要将DOCLD_LIBRARY_PATH扩展到lib所在的位置:/opt/dev/versions/vtk/vtk-7.1.0-shared/lib/.
>如果我从终端启动python,我可以成功导入vtk.

import vtk
v = vtk.vtkVersion()
print v.GetVTKVersion()

>如果我尝试在PyCharm的python控制台中导入vtk,我会收到以下错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-b7e11aadda62>", line 1, in <module>
    import vtk
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vtk/__init__.py", line 41, in <module>
    from .vtkCommonCore import *
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/vtk/vtkCommonCore.py", line 9, in <module>
    from vtkCommonCorePython import *
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: No module named vtkCommonCorePython

到目前为止,据我所知,问题是由El Capitan引入的System Integrity Protection(SIP)引起的.其中一个影响是子进程只有restricted access到其他资源,而且很可能,PyCharm将python作为单独的进程执行.

我也明白python无法导入vtk,因为它无法找到python模块链接到的dylib.我可以通过两种方式验证这一点:

> DYLD_LIBRARY_PATH为空.这是因为python在PyCharm中作为子进程运行:os.getenv(‘DYLD_LIBRARY_PATH’)返回None.
>当我将所有库从/opt/dev/versions/vtk/vtk-7.1.0-shared/lib/复制到当前工作目录时,我可以导入模块

现在的问题是:显然,DYLD_LIBRARY_PATH不能在子进程中使用,因此自El Capitan以后就不应再使用它了.那么,如何正确地取代在MacOS 10.11之前完美运行的“连接黑客”.有没有办法继续使用DYLD_LIBRARY_PATH?

禁用SIP不是一种选择.显然,将dylib复制到当前工作目录会有所帮助,但这对我来说是不可行的.然而,将lib放在站点包位置(vtk)并没有帮助.

我很确定很多人一直依赖于DYLD_LIBRARY_PATH-hack而现在正在努力解决SIP的后果 – 这就是为什么我认为社区可能从这个相当冗长的问题中受益.

解决方法:

经过长时间的斗争,我能够解决问题的最后一点.

通过为已安装的二进制文件的RPATH 070​​00设置固定值,我的链接问题就消失了.

实现这一目标有不同的可能性.我想一个选项是使用install_name_tool.对我来说,最简单的是用适当的CMake标志构建vtk.这里我更新了对cmake的调用,其中CMAKE_MACOSX_RPATH和CMAKE_INSTALL_RPATH有所不同:

    cmake -G Ninja .. -DCMAKE_BUILD_TYPE=Release \
              -DVTK_WRAP_PYTHON=ON \
              -DBUILD_EXAMPLES=OFF \
              -DBUILD_SHARED_LIBS=ON \
              -DBUILD_TESTING=OFF \
              -DCMAKE_INSTALL_PREFIX="/opt/dev/versions/vtk/vtk-7.1.0-shared" \
              -DCMAKE_MACOSX_RPATH=ON \
              -DCMAKE_INSTALL_RPATH="/opt/dev/versions/vtk/vtk-7.1.0-shared/lib" \
              -DPYTHON_INCLUDE_DIR="/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/" \
              -DPYTHON_LIBRARY="/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib"

阅读here关于CMake的rpath处理的更多信息.请注意,otool -L vtkCommonCorePython.so(例如)仍将在输出中写入@rpath,但该值仍然是固定的.

@rpath/libvtkCommonCorePython27D-7.1.1.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libvtkWrappingPython27Core-7.1.1.dylib (compatibility version 0.0.0, current version 0.0.0)
/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
@rpath/libvtksys-7.1.1.dylib (compatibility version 0.0.0, current version 0.0.0)
@rpath/libvtkCommonCore-7.1.1.dylib (compatibility version 0.0.0, current version 0.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)

标签:python,macos,vtk,dyld
来源: https://codeday.me/bug/20190627/1309692.html