B.
Appendix B: Python Environment Setup
Written by Derek Selander
It’s not my place to force an IDE on you for Python development. However, if you’re actively looking for a Python editor for the Python related chapters — then we should have a little chat.
Getting Python
Good news: if you have a Mac, it automatically ships (at the time of writing) with Python version 2.7. This is the same version LLDB uses.
If, for some weird reason, you like to rm random things in Terminal and you need to reinstall Python, you can download Python here: https://www.python.org/downloads/. 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
Don’t worry about the final part of the version number. If you have 2.7.12 and LLDB quotes 2.7.10, that will work just fine.
Python text editors
A list of Python editors can be found here: https://wiki.python.org/moin/PythonEditors.
For the small, quick Python scripts you’ll write in this book, I would recommend using Sublime Text. Sublime Text can be found at https://www.sublimetext.com/; although it’s a paid application, it’s free to try with no time limit.
Both this book, as well as all my LLDB Python scripts, were written (and debugged) through Sublime Text 3.
You’ll likely want to install a couple of additional components to Sublime Text to make developing and debugging LLDB Python scripts easier.
The easiest way to install these additional components is to use the Sublime Text Package Control, which is an excellent package manager for Sublime Text. You can find instructions on how to install the Package Control at https://packagecontrol.io/installation.
Once installed, you’ll be able to easily search for new components designed for Sublime Text by pressing ⌘ + Shift + P and typing install. A selection item of Package Control: Install Package will appear.
Select this option:
After the package manager has been installed, you can search for packages that will help you in Python development. Here are a few packages I would recommend using if you’re developing in Python:
-
AutoPep8: Automatically formats Python code to conform to the PEP 8 style guide using
autopep8andpep8modules. https://packagecontrol.io/packages/AutoPEP8 -
PythonBreakpoints: A Sublime Text plugin to quickly set Python breakpoints by injecting the
set_trace()call ofpdbor another debugger of your choice. https://packagecontrol.io/packages/Python%20Breakpoints -
Anaconda: Anaconda turns your Sublime Text 3 into a fully featured Python development IDE including autocompletion, code linting,
autopep8formatting,McCabecomplexity checkerVagrantandDockersupport for Sublime Text 3 usingJedi,PyFlakes,pep8,MyPy,PyLint,pep257— andMcCabewill never freeze your Sublime Text 3. https://packagecontrol.io/packages/Anaconda
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 where the lldb module is located 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 Sublime Text. As a result, you’ll need to modify your PYTHONPATH environment variable to include the appropriate directory where the lldb Python module lives.
In Terminal, ensure your ~/.bash_profile exists:
touch ~/.bash_profile
Open .bash_profile file in your favorite text editor (like Sublime!) and add the following line of code:
export PYTHONPATH=/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/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.
Save and close the file. You’ll 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 Sublime Text (or equivalent) during debugging time — instead of finding a syntax error when your script is loaded into LLDB.