Neal.Marlin
Neal.Marlin

Reputation: 508

How to import lldb module for python on Mac?

I need a lldb python library to debug my python script. I made my python environment configuration following the lldb.llvm.org's instructions. But I got some errors as follow:

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

And the PyCharm project structure is as picture showing blow:
PyCharm Screenshot

Upvotes: 1

Views: 3469

Answers (3)

yuhui su
yuhui su

Reputation: 11

About installing or using lldb python lib in mac.

If you are using system python: /usr/bin/python3

If you are currently using the ORIGIN python in your system, you should probably use the lldb inside: /Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Resources/Python

However the path might be different form what I just provided. So here are some steps to ascertain what lldb path you should focus.

  1. In your terminal, run which python and which lldb, you should make sure the current python and lldb is under the same folder: /usr/bin/
  2. If so, run lldb -P to see the lldb python library, it should already be in your mac
  3. Then, config your path, add the following path into your zshrc or bash_profile depending on the shell which your are using for now:
# PYTHONPATH is an environment variable that is used to 
# specify the location of Python libraries. It is typic-
# ally used by developers to ensure that their code can
# find the required Python libraries.
PYTHONPATH=`lldb -P`
  1. Remember to run source ~/.zshrc, then you could run python, then you could see the following output:
➜  ~ /usr/bin/python3
Python 3.9.6 (default, Aug 11 2023, 19:44:49)
[Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lldb
>>>

If you are using conda python: /Users/username/opt/miniconda3/bin/python

You could just run conda install -c conda-forge lldb inside your terminal. You could also refer to this webpage: https://anaconda.org/conda-forge/lldb.

Then you could test your lldb.

(base) ➜  ~ python
Python 3.9.18 | packaged by conda-forge | (main, Aug 30 2023, 03:53:08)
[Clang 15.0.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lldb
>>>

Hope it helps!

Upvotes: 0

rustyMagnet
rustyMagnet

Reputation: 4085

On MacOS PyCharm go Preferences\Python Interpreter\ Then click on the Settings buttons and Show All.

enter image description here

enter image description here

Other answers said you need this:

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

With the above setting, it worked with just import lldb.

Upvotes: 0

Jim Ingham
Jim Ingham

Reputation: 27110

The lldb python module shipped with Xcode builds against a specific version of Python.

Prior to Xcode 11 lldb was built against the Python2.7.1 in /System/Library/Frameworks. Starting with Xcode 11, lldb is built against the version of Python 3 (currently 3.7.3) that ships with the Xcode from which you got your lldb. You can locate the proper python3 command line tool by running xcrun python3.

We haven't had much success getting the lldb module we build against this 3.7.3 Python to load into other hand-built Pythons. I'm not sure that this is particularly well supported by Python, though I don't know of anybody who has looked into what it would take to support this.

We do use a lot of the Python C API's in the lldb bindings, so we are more bound to the Python version than pure Python modules. Anyway, at present if you need to load the lldb module into a python you have installed from elsewhere, you will most likely need to hand-build lldb against that python library.

Upvotes: 4

Related Questions