The `clang` package provides Python bindings for libclang, enabling programmatic interaction with Clang's C/C++/Objective-C abstract syntax trees (ASTs), parsing source code, and accessing compiler information. It is maintained as part of the broader LLVM project and typically releases in sync with major LLVM versions. The current version is 21.1.7.
pip install clangVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse C code from a string, check for diagnostics, and traverse the Abstract Syntax Tree (AST) using `clang.cindex`. The most critical step is ensuring that the underlying `libclang` shared library is installed on your system and discoverable by the Python bindings.
Install `libclang` via your system's package manager (e.g., `apt install libclang-dev` on Debian/Ubuntu, `brew install llvm` on macOS, or LLVM installer on Windows) before running Python code.
Set the `CLANG_LIBRARY_PATH` environment variable to the directory containing `libclang` before running your Python script, or call `clang.cindex.Config.set_library_path('/path/to/libclang/directory')` in your code.Try to keep the Python `clang` package version in sync with your system's `libclang` version. For example, `pip install 'clang==18.*'` if you have LLVM 18 installed. Downgrade/upgrade `libclang` or the Python bindings if problems persist.
Always refer to the official LLVM `libclang` Python binding documentation or examples corresponding to your LLVM version when upgrading. Test thoroughly after upgrading major versions.
Pass all necessary compiler flags (e.g., `-I`, `-D`, `-std=c++17`, `-x c++`) as a list of strings to the `args` parameter of `index.parse()`.
Ensure the LLVM/Clang development packages are installed (e.g., `apt-get install libclang-dev` on Debian/Ubuntu, `brew install llvm` on macOS). Explicitly set the path to the library in your Python script: `from clang.cindex import Config; Config.set_library_file('/path/to/libclang.so')` (or `.dll`/`.dylib` as appropriate). If you installed `libclang` via pip, verify its installation and check the package documentation for specific setup instructions if the automatic discovery fails.Install the `clang` package using pip: `pip install clang`. If you are using a virtual environment, ensure it is activated. Verify that the Python interpreter you are running is the one where the package was installed.
Access children by iterating over the `Cursor` object itself, or by calling `get_children()` as an iterator. For example: `for child in cursor.get_children():`. Ensure you are using a Python 3 compatible version of the `clang` bindings, as some older versions had compatibility issues.
No dependency data recorded yet.