Chapters

Hide chapters

Advanced Apple Debugging & Reverse Engineering

Fourth Edition · iOS 16, macOS 13.3 · Swift 5.8, Python 3 · Xcode 14

Section I: Beginning LLDB Commands

Section 1: 10 chapters
Show chapters Hide chapters

Section IV: Custom LLDB Commands

Section 4: 8 chapters
Show chapters Hide chapters

B. Appendix B: Python Environment Setup
Written by Walter Tyree

If you’re actively looking for a Python editor for the Python-related chapters, here are some recommendations.

Getting Python

MacOS no longer ships with Python installed. However, if you open a terminal window and attempt to launch Python, the system will offer to download and install Apple’s preferred version along with the other command line developer tools. In Terminal, type python3, and your computer will either launch the interactive Python shell or present a dialog like the one below:

If, for some reason, you like to rm random things in Terminal and you need to reinstall Python, you can download Python from the official download page or using the homebrew package manager. Make sure to download the version of Python that matches the version packaged with LLDB. If you’re not sure which version to get, you can get the LLDB Python version through Terminal:

% lldb
(lldb) script import sys; print(sys.version)

You’ll see something like this:

3.9.6 (default, Mar 10 2023, 20:16:38)
[Clang 14.0.3 (clang-1403.0.22.14.1)]

This is from Xcode version 14.3. Your output may vary if you have a newer Xcode installed. You can ignore the Clang version there, as that’s not relevant. What’s relevant is the 3.9.6.

Don’t worry about the final part of the version number. As long as you have some version of Python 3, you’ll be fine. There are many breaking changes between versions 2 and 3.

Python Text Editors

The official Python website maintains a list of Python editors. According to my daughter, who’s studying CS at university (Hi, Kitty!), the cool Python developers who want a free editor use either Visual Studio Code (VS Code) or Vim.

For the small, quick Python scripts you’ll write in this book and for an experience that approximates Xcode, I’d recommend using Microsoft’s Visual Studio Code. VS Code is a general-purpose editor that’s become pretty popular recently for Python and also with developers who write Swift on Linux. You can download it from the VS Code website.

Above, is a screenshot of the VS Code editor, editing a python file in this case keychain.py.

All the LLDB Python scripts in this book have been edited and tested in VS Code.

You’ll want to add a Python support extension to make developing and debugging LLDB Python scripts easier. Fortunately, Microsoft maintains an official extension to provide code completion, linting, debugging and more for Python code.

To add extensions to VS Code, press ⌘-Shift-X to switch to the Extensions view, and then type “python” in the search box. You’ll see quite a few choices, but find the one from Microsoft, and select it. Then click Install.

Another popular option is to use Vim. Even if you decide to use VS Code or some other editor, I highly recommend getting at least a working knowledge of Vim. It’s installed by default on many *nix systems, including macOS. Though it doesn’t come installed by default, it’s usually very easy to install. So, when you’re coding away from home, you can be sure it’s available. This can be really important if you’re on a remote web server somewhere or sitting on the floor of a cold data center in the middle of the night as you try to get the company’s systems back online.

As with VS Code, extensions are available that make working with Python code easier. Unlike VS Code, which was released in 2015 and is guided by Microsoft, Vim and its predecessor, vi, have been around since the original Star Wars trilogy was first released in theaters. There are lots of package managers and Python extensions — in Vim, they’re called plugins — available. Do a web search for “python vim ide”, and you’ll find a number of excellent tutorials and resources to get you going.

Working With the LLDB Python Module

When working with Python, you’ll often import modules to execute code or classes within that module. When working with LLDB’s Python module, you’ll sometimes come across an import lldb somewhere in the script, usually right at the top.

By default, Xcode will launch a version of Python that’s bundled within Xcode. When Xcode launches this bundled version of Python, the path to the lldb module’s location is set up automatically. However, in your normal Python development, you won’t have access to this module if you were to execute your script through VS Code or Vim. As a result, you’ll need to modify your PYTHONPATH environment variable to include the appropriate directory where the LLDB Python module lives.

If you’re not familiar with updating your Terminal environment, open a Terminal window and find which shell you’re using. It will be shown in the title bar of the terminal window and is probably zsh or bash. If you don’t see either of those in the title bar, type this command:

% echo $0

Terminal will respond with the name of your current shell. Now, in your home directory, use the touch command for a file named .bash_profile if you use bash, or .zshrc if you have zsh. This will create the file if it doesn’t already exist.

% touch ~/.bash_profile

Or:

% touch ~/.zshrc

Open the file you just created in your favorite text editor, and add the following line of code:

export PYTHONPATH="/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python:$PYTHONPATH"

Note: This assumes your Xcode is located at /Applications/Xcode.app. If it isn’t, because you particularly like being different, then you’ll need to change the path. Just type xcode-select -p in Terminal to find the correct path to your Xcode.

Save and close the file. Now, restart Terminal so it loads your changes. Confirm everything works by typing:

% echo $PYTHONPATH

Terminal should show the path you just added. You’ll now be able to access the lldb module from any Python session on your computer.

Doing this gives you the advantage of checking for syntax errors in VS Code (or equivalent) during debugging time — instead of finding a syntax error when your script is loaded into LLDB.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.