如何在 Mac 上为 python 导入 lldb 模块?

How to import lldb module for python on Mac?

我需要一个 lldb python 库来调试我的 python 脚本。我按照 lldb.llvm.org 的说明进行了 python 环境配置。但是我得到了一些错误如下:

/Users/heping/Desktop/Scripts/.env/python-3.7.3/bin/python /Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 57996 --file /Users/heping/Desktop/Scripts/RevealServerCommands.py
pydev debugger: process 59879 is connecting

Connected to pydev debugger (build 193.5662.61)
Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py", line 35, in <module>
    import _lldb
ModuleNotFoundError: No module named '_lldb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py", line 38, in <module>
    from . import _lldb
ImportError: dynamic module does not define module export function (PyInit__lldb)

而PyCharm项目结构如图所示:

Xcode 随附的 lldb python 模块针对 Python 的特定版本构建。

在 Xcode 之前,11 lldb 是针对 /System/Library/Frameworks 中的 Python2.7.1 构建的。从 Xcode 11 开始,lldb 是针对 Python 3(当前为 3.7.3)版本构建的,该版本随 Xcode 一起提供,您从中获得了 lldb。您可以通过 运行 xcrun python3.

找到合适的 python3 命令行工具

我们在将我们针对此 3.7.3 Python 构建的 lldb 模块加载到其他手动构建的 Python 中并没有取得多大成功。我不确定 Python 是否特别支持这一点,但我不知道有谁研究过支持这一点需要什么。

我们确实在 lldb 绑定中使用了很多 Python C API,所以我们更多地绑定到 Python 版本而不是纯 Python模块。无论如何,目前如果您需要将 lldb 模块加载到从其他地方安装的 python 中,您很可能需要针对该 python 库手动构建 lldb。

在 MacOS 上 PyCharm 转到 Preferences\Python Interpreter\ 然后单击“设置”按钮和 Show All

其他回答说你需要这个:

import sys
sys.path.append('/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python3')
import lldb

通过上述设置,它仅适用于 import lldb