Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import LLDB in Python 3 on Mac?

I'm struggling to find good documentation on this, and I haven't even been able to confirm whether LLDB is supposed to work with python 3 or not (see LLDB-Python reference)

When trying to load it as follows:

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

This works for python 2 (2.7 to be exact), but in python 3 (3.6 to be exact), I get an error (see full strack trace at end):

ImportError: dynamic module does not define module export function (PyInit__lldb)

Is there anything I can do to be able to import lldb in Python 3?


Full stack:

ImportError                               Traceback (most recent call last)
<ipython-input-3-2a8839b33e40> in <module>()
----> 1 import lldb

/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py in <module>()
     48                 fp.close()
     49             return _mod
---> 50     _lldb = swig_import_helper()
     51     del swig_import_helper
     52 else:

/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python/lldb/__init__.py in swig_import_helper()
     44         if fp is not None:
     45             try:
---> 46                 _mod = imp.load_module('_lldb', fp, pathname, description)
     47             finally:
     48                 fp.close()

~/Virtualenvs/py36/lib/python3.6/imp.py in load_module(name, file, filename, details)
    240                 return load_dynamic(name, filename, opened_file)
    241         else:
--> 242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:
    244         return load_package(name, filename)

~/Virtualenvs/py36/lib/python3.6/imp.py in load_dynamic(name, path, file)
    340         spec = importlib.machinery.ModuleSpec(
    341             name=name, loader=loader, origin=path)
--> 342         return _load(spec)
    343 
    344 else:

ImportError: dynamic module does not define module export function (PyInit__lldb)
like image 675
Julien Marrec Avatar asked Sep 02 '17 09:09

Julien Marrec


People also ask

How do I set up Python on Mac?

Install Python 3 with the Official Installer First, download an installer package from the Python website. To do that, visit https://www.python.org/downloads/ on your Mac; it detects your operating system automatically and shows a big button for downloading the latest version of Python installer on your Mac.

Can LLDB debug Python?

The entire LLDB API is available as Python functions through a script bridging interface. This means the LLDB API's can be used directly from python either interactively or to build python apps that provide debugger features.

Where is Python installed on Mac?

The Apple-provided build of Python is installed in /System/Library/Frameworks/Python. framework and /usr/bin/python , respectively.


2 Answers

As I understand it, that will not work. LLDB is built against Python 2.7 and you can't import Python 2.x modules into Python 3.x.

It is possible to build your own copy of lldb against the Python 3.x libraries - we needed to do that way for Windows so the sources are set up to build with either version.

I've never tried it on the Mac, and the Xcode project isn't currently set up to make this work, but it shouldn't be that hard to do. If you are interested in this and have more questions, the lldb-dev mailing list is the best place to ask them:

http://lists.llvm.org/mailman/listinfo/lldb-dev

like image 172
Jim Ingham Avatar answered Sep 16 '22 16:09

Jim Ingham


After reading this (https://lldb.llvm.org/resources/caveats.html), I got it to work on 10.15.4, using XC 11.5.

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

On my machine, I tested and I did not need to do the xcrun python3 (python3 alone was sufficient), but I left it in because of the listed caveat. The key is to use the lldb package specifically built for python3.

like image 21
Pat Avatar answered Sep 19 '22 16:09

Pat