[英]Strange mixing of system + homebrew Python with LLDB
当我尝试在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.
当我检查启动了哪个版本的Python时,Python报告它应该是Homebrew Python(它被符号链接到这个位置):
>>> sys.executable
'/usr/local/opt/python/bin/python2.7'
但是,询问Python版本会返回与默认系统 Python安装相关联的版本,例如
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
并且,只是为了确认,上面的二进制路径上的Python版本确实不同(注意微版本的差异):
$ /usr/local/opt/python/bin/python2.7 --version
Python 2.7.14
$ /usr/bin/python --version
Python 2.7.10
为了让事情更加混乱,名称_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>
有什么想法可能导致我的Python安装与LLDB之间的这种明显的串扰? 我怎么能阻止这个?
此问题的一个解决方法是使用PATH上的系统Python安装显式启动LLDB,例如
PATH=/usr/bin /usr/bin/lldb
似乎LLDB在PATH
查询“活动”Python安装; 如果您在PATH
上有可用的Homebrew安装Python,那么当LLDB尝试启动Python时,您可能遇到这种串扰。
如果我们使用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
但是,如果我们告诉DYLD专门从Homebrew加载Python.framework,那么它可以工作:
DYLD_FRAMEWORK_PATH="$(brew --prefix python@2)/Frameworks/" "$(xcrun -f lldb)"
使用brew'd Python 2.7.15在macOS 10.13.6上测试。
更新:启用系统完整性保护时,可能会禁止在从/usr/bin
运行的Xcode命令行工具trampoline可执行文件中使用DYLD_FRAMEWORK_PATH
。 要解决这个问题,我们运行xcrun -f lldb
来代替使用真正的LLDB可执行文件。
你可以简单地运行brew unlink python@2
而不是卸载它。 这将从您的PATH中删除Homebrew的Python 2.x. 很多Homebrew公式依赖于Homebrew的Python 2.x,所以你可以保留brew安装的Python 2.x,同时用lldb修复错误。
如果您使用Homebrew Bundle管理Homebrew设置,则可以将此行添加到Brewfile
:
brew "python@2", link: false
我已经从Homebrew卸载python@2
解决了这个问题: https : //github.com/flutter/flutter/issues/17803#issuecomment-390980648
UPD:
正如@Olsonist在评论中指出的那样,运行此命令必须解决这个问题: brew uninstall --force python@2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.