简体   繁体   English

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

[英]Strange mixing of system + homebrew Python with LLDB

When I attempt to run the Python interpretter within lldb , I'm seeing: 当我尝试在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.

When I inspect what version of Python was launched, Python reports that it should be the Homebrew Python (which is symlinked into this location): 当我检查启动了哪个版本的Python时,Python报告它应该是Homebrew Python(它被符号链接到这个位置):

>>> sys.executable
'/usr/local/opt/python/bin/python2.7'

However, asking the Python version returns the version associated with the default system Python installation, eg 但是,询问Python版本会返回与默认系统 Python安装相关联的版本,例如

>>> sys.version_info
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)

And, just to confirm, the Python version at the binary path above is indeed different (note the difference in the micro version): 并且,只是为了确认,上面的二进制路径上的Python版本确实不同(注意微版本的差异):

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

$ /usr/bin/python --version
Python 2.7.10

To make things more confusing, the name _remove_dead_weakref does exist in the _weakref module for my Homebrew Python installation, but not the default system installation: 为了让事情更加混乱,名称_remove_dead_weakref 确实存在于我的Homebrew Python安装的_weakref模块中,但不是默认的系统安装:

$ /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>

Any idea what could be causing this apparent cross-talk between my Python installations with LLDB? 有什么想法可能导致我的Python安装与LLDB之间的这种明显的串扰? How can I prevent this? 我怎么能阻止这个?

One workaround to this issue is to explicitly launch LLDB with only the system Python installation on the PATH, eg 此问题的一个解决方法是使用PATH上的系统Python安装显式启动LLDB,例如

PATH=/usr/bin /usr/bin/lldb

It appears as though LLDB queries the PATH for the 'active' Python installation; 似乎LLDB在PATH查询“活动”Python安装; if you have a Homebrew installation of Python available on the PATH then you can run into this sort of cross-talk when LLDB attempts to launch Python. 如果您在PATH上有可用的Homebrew安装Python,那么当LLDB尝试启动Python时,您可能遇到这种串扰。

If we run lldb with DYLD_PRINT_LIBRARIES=1 set, then we can see that it's loading the Python.framework from /System/Library, but then some other Python libraries from Homebrew: 如果我们使用DYLD_PRINT_LIBRARIES=1 set运行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

But if we tell DYLD to specifically load Python.framework from Homebrew, then it works: 但是,如果我们告诉DYLD专门从Homebrew加载Python.framework,那么它可以工作:

DYLD_FRAMEWORK_PATH="$(brew --prefix python@2)/Frameworks/" "$(xcrun -f lldb)"

Tested on macOS 10.13.6 with brew'd Python 2.7.15. 使用brew'd Python 2.7.15在macOS 10.13.6上测试。

Update: When System Integrity Protection is enabled, using DYLD_FRAMEWORK_PATH may be prohibited on the Xcode Command Line Tools trampoline executables running from /usr/bin . 更新:启用系统完整性保护时,可能会禁止在从/usr/bin运行的Xcode命令行工具trampoline可执行文件中使用DYLD_FRAMEWORK_PATH To work around that, we run xcrun -f lldb to use the real LLDB executable instead. 要解决这个问题,我们运行xcrun -f lldb来代替使用真正的LLDB可执行文件。

You can simply run brew unlink python@2 instead of uninstalling it. 你可以简单地运行brew unlink python@2而不是卸载它。 This will remove Homebrew's Python 2.x from your PATH. 这将从您的PATH中删除Homebrew的Python 2.x. A lot of Homebrew formulas depend on Homebrew's Python 2.x, so you can keep the brew-installed Python 2.x while also fixing the error with lldb. 很多Homebrew公式依赖于Homebrew的Python 2.x,所以你可以保留brew安装的Python 2.x,同时用lldb修复错误。

If you manage your Homebrew setup with Homebrew Bundle, you can add this line to your Brewfile : 如果您使用Homebrew Bundle管理Homebrew设置,则可以将此行添加到Brewfile

brew "python@2", link: false

I've solved this problem with uninstalling python@2 from Homebrew: https://github.com/flutter/flutter/issues/17803#issuecomment-390980648 我已经从Homebrew卸载python@2解决了这个问题: https//github.com/flutter/flutter/issues/17803#issuecomment-390980648

UPD: UPD:

As @Olsonist noted in comments, running this command must fix this problem: brew uninstall --force python@2 正如@Olsonist在评论中指出的那样,运行此命令必须解决这个问题: brew uninstall --force python@2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM