How to import lldb module for python on Mac?
Asked Answered
K

3

1

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

Krusche answered 4/1, 2020 at 14:9 Comment(0)
P
4

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.

Principality answered 6/1, 2020 at 19:9 Comment(7)
Thanks for your answer. Do you mean that I could solve this problem by changing my python interpreter to ‘xcrun python3’ to be compatible with Xcode lldb version, or otherwise, bulding lldb against the specific python interpreter that I use? Right?Krusche
Yes, either of those would work, though the former - if possible - will be much simpler.Principality
Thank you so much and I will check it out.Krusche
I've try the former, and it works really well. Thank you. But I have another question: How could I debug my script under Xcode lldb, or in another word, how could I attach a python debugger with my script under the lldb environment. Any clue will be much appreciated.Krusche
I don't have much experience with Python debuggers. When I want to debug Python under lldb (for instance to debug lldb Python data formatters and the like) I just modify the Python code, and add import pdb; pdb.set_trace().Principality
I haven't explored how to get external debuggers to work. If you had a Python debugger that could attach to arbitrary interpreters, that would work but I don't know if that's possible. If there's an open source debugger you can build it against Xcode's Python.framework. It might be better to ask this as a separate general Python question: "How to I attach a Python debugger to some externally provided python interpreter". Somebody in the Python community will know whether that's possible or not.Principality
OK. Thank you so much. I know what you mean. And I think I just rely too much on the IDE, I could just use the built in debugger module. But what I’m asking may be if it’s possible to debug python remotely, just like Node.js. If it’s possible I could use my IDE to deal with it. I will find out it or ask another question. Thanks again. : )Krusche
M
0

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.

Mashie answered 23/10, 2020 at 8:29 Comment(6)
Yes, you’re telling how to change Python Interpreter and lldb Python library to the Xcode specific version. I’ve successfully done that. But a new problem I face is how to debug. Because the Python scripts is not execute directly, it is invoked by the Xcode lldb process, I don’t know how to attach a Python debugger to that process. I’ve tried to use the Python internal debugger module pdb. But when I use the internal debugger command from the Xcode lldb console, lldb get stuck, and I don’t know why. Do you know how to solve this debugging problem? Any clue will be appreciated.Krusche
are you connecting from Terminal lldb attach -p $(ps x | grep -i -m1 my_app | awk '{print $1}')? That will connect to an iOS simulator app from the command line called my_appMashie
Oh, I haven't try this way before. I just use a Xcode debugger condition break point to execute lldb command to load my python script, or just pause the debugger process and the execute lldb command to load my python script. I would try your way to find out if the pdb command could work properly.Krusche
I would like to get pdb working properly. But if it helps, I wrote quite a few breakpoints examples on here: github.com/rustymagnet3000/…Mashie
@rustMagnet I read your GitHub project few minutes ago. It's awesome. But I have a question, that how you debug your Python script. Would you please help me with this? Thank you.Krusche
Hi @Krusche I found myself writing scripts line by line in the script bridge, so I know they work, before putting them into a full script. lldb) script Not perfect but it helps me find errors quickly.Mashie
N
0

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!

Negligent answered 8/10, 2023 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.