[英]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.