将系统+自制软件与LLDB混合在一起

Kev*_*hey 20 python macos homebrew lldb

当我试图在其中运行Python解释器时lldb,我看到:

$ lldb
(lldb) script
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 52, in <module>
    import weakref
  File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
Run Code Online (Sandbox Code Playgroud)

当我检查启动了哪个版本的Python时,Python报告它应该是Homebrew Python(它被符号链接到这个位置):

>>> sys.executable
'/usr/local/opt/python/bin/python2.7'
Run Code Online (Sandbox Code Playgroud)

但是,询问Python版本会返回与默认系统 Python安装相关联的版本,例如

>>> sys.version_info
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)

并且,只是为了确认,上面的二进制路径上的Python版本确实不同(注意微版本的差异):

$ /usr/local/opt/python/bin/python2.7 --version
Python 2.7.14

$ /usr/bin/python --version
Python 2.7.10
Run Code Online (Sandbox Code Playgroud)

为了使事情变得更加混乱,名称_remove_dead_weakref 存在在_weakref我的自制Python安装模块,而不是默认的系统安装:

$ /usr/bin/python -c "import _weakref; print _weakref._remove_dead_weakref"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '_remove_dead_weakref'

$ /usr/local/opt/python/bin/python2.7 -c "import _weakref; print _weakref._remove_dead_weakref"
<built-in function _remove_dead_weakref>
Run Code Online (Sandbox Code Playgroud)

有什么想法可能导致我的Python安装与LLDB之间的这种明显的串扰?我怎么能阻止这个?

Kev*_*hey 14

此问题的一个解决方法是使用PATH上的系统Python安装显式启动LLDB,例如

PATH=/usr/bin /usr/bin/lldb
Run Code Online (Sandbox Code Playgroud)

似乎LLDB查询PATH"活动"Python安装; 如果您有可用的Homebrew安装Python,PATH那么当LLDB尝试启动Python时,您可能遇到这种串扰.

  • 对我来说,`brew unlink python @ 2`似乎也可以。我在PATH中不需要自制的“ python”可执行文件。 (2认同)

rgo*_*gov 7

如果我们使用DYLD_PRINT_LIBRARIES=1set 运行lldb ,那么我们可以看到它正在从/ System / Library加载Python.framework,然后从Homebrew加载其他Python库:

dyld: loaded: /System/Library/Frameworks/Python.framework/Versions/2.7/Python
...
dyld: loaded: /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_locale.so
Run Code Online (Sandbox Code Playgroud)

但是,如果我们告诉DYLD从Homebrew专门加载Python.framework,那么它将起作用:

DYLD_FRAMEWORK_PATH="$(brew --prefix python@2)/Frameworks/" "$(xcrun -f lldb)"
Run Code Online (Sandbox Code Playgroud)

已在带有brew'd Python 2.7.15的macOS 10.13.6上进行了测试。

更新:启用系统完整性保护后,DYLD_FRAMEWORK_PATH可能会禁止从处运行Xcode命令行工具蹦床可执行文件/usr/bin。要解决此问题,我们运行xcrun -f lldb改为使用真正的LLDB可执行文件。