Skip to main content

Clang Python Bindings, mirrored from the official LLVM repo: https://github.com/llvm/llvm-project/tree/main/clang/bindings/python.

Project description

libclang-ng

PyPI Python Downloads License Arch: x64 Arch: aarch64

Linux x64 Linux Arm64 MacOS x64 MacOS Arm64 Windows x64 Windows Arm64

libclang-ng packages the official Clang Python Bindings (clang.cindex) from the LLVM project and bundles a statically-linked libclang shared library for each supported platform. The result is a zero-configuration pip install that gives you a working Clang Python API without installing the LLVM toolchain.

Note: This package is named libclang-ng, and it's the natural evolution of libclang. The clang package on PyPI is a separate project and does not bundle a prebuilt shared library.

Installation

Install using pip:

pip install libclang-ng==20.1.8.1

Install using uv:

uv pip install libclang-ng==20.1.8.1

Best way to specify pinning is to allow for patch updates on the same clang version:

pip install "libclang-ng>=20.1.8.0,<21.0.0.0" --upgrade

Requirements

  • Python 3.12 or later

Platform Support

Platform Architecture Minimum OS
Linux x86_64 glibc 2.28+ (manylinux_2_28)
Linux aarch64 glibc 2.28+ (manylinux_2_28)
macOS x86_64 10.9+
macOS arm64 11.0+
Windows x86_64
Windows arm64

All native libraries are statically linked — there are no external runtime dependencies beyond the standard system libraries (glibc on Linux, system frameworks on macOS, MSVC runtime on Windows).

Clang Versions

Supported clang versions:

Quick Start

Parse a file and walk the AST

from clang.cindex import Index, CursorKind

index = Index.create()
tu = index.parse("example.cpp", args=["-std=c++17"])

for cursor in tu.cursor.get_children():
    if cursor.kind == CursorKind.FUNCTION_DECL:
        print(f"Function: {cursor.spelling} at {cursor.location}")
        for param in cursor.get_arguments():
            print(f"  param: {param.spelling} ({param.type.spelling})")

Parse from a string (in-memory)

from clang.cindex import Index

index = Index.create()
tu = index.parse(
    "example.cpp",
    unsaved_files=[("example.cpp", "int add(int a, int b) { return a + b; }")],
    args=["-std=c++17"],
)

for cursor in tu.cursor.walk_preorder():
    print(f"{cursor.kind.name}: {cursor.spelling}")

Collect diagnostics

from clang.cindex import Index, Diagnostic

index = Index.create()
tu = index.parse("example.cpp")

for diag in tu.diagnostics:
    if diag.severity >= Diagnostic.Warning:
        print(f"{diag.severity}: {diag.spelling} [{diag.location}]")

Type introspection

from clang.cindex import Index, CursorKind, TypeKind

index = Index.create()
tu = index.parse("example.cpp")

for cursor in tu.cursor.walk_preorder():
    if cursor.kind == CursorKind.STRUCT_DECL:
        t = cursor.type
        print(f"struct {cursor.spelling}: size={t.get_size()} bytes")
        for field in t.get_fields():
            print(f"  {field.spelling}: {field.type.spelling} offset={t.get_offset(field.spelling)} bits")

Use a compilation database

from clang.cindex import Index, CompilationDatabase

db = CompilationDatabase.from_directory("/path/to/build")
cmds = db.get_compile_commands("src/main.cpp")

index = Index.create()
for cmd in cmds:
    tu = index.parse(cmd.filename, args=list(cmd.arguments)[1:])

API Overview

Class Purpose
Index Entry point — creates and owns translation units
TranslationUnit Represents a parsed source file; holds cursors and diagnostics
Cursor A node in the AST (declaration, expression, statement, …)
CursorKind Enumeration of all cursor node types
Type Type information attached to a cursor
TypeKind Enumeration of all type kinds
Diagnostic A compiler warning or error with location and fix-it hints
Token A lexical token (keyword, identifier, literal, …)
SourceLocation File/line/column position in source
SourceRange Start and end SourceLocation pair
CompilationDatabase Reads compile_commands.json for project-wide parsing

Full API documentation is generated from the module docstrings via Sphinx and is available at libclang-ng.readthedocs.io.

Configuration

By default the package locates libclang automatically from its bundled native/ directory. Override this when you want to use a system-installed or custom-built library:

Environment variable (before importing clang):

export LIBCLANG_LIBRARY_PATH=/usr/lib/llvm-19/lib
python your_script.py

Programmatic override (before any other clang import):

from clang.cindex import Config
Config.set_library_path("/usr/lib/llvm-19/lib")
# or point to a specific file:
# Config.set_library_file("/usr/lib/llvm-19/lib/libclang.so.19")

Development

The repository uses just as a task runner and uv as the Python package manager.

# Show the current LLVM version
just version

# Download LLVM source and update Python bindings
just download-llvm

# Full local build for macOS ARM64
just build-macos-arm64

CI / Release

Six GitHub Actions workflows build and publish a platform-specific wheel for each supported target. A release is triggered by pushing a version tag (v*), which causes each workflow to publish its wheel to PyPI.

License

This repository follows the license of the LLVM project: Apache-2.0 WITH LLVM-exception.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

libclang_ng-21.1.8.1-py3-none-win_arm64.whl (19.1 MB view details)

Uploaded Python 3Windows ARM64

libclang_ng-21.1.8.1-py3-none-win_amd64.whl (21.8 MB view details)

Uploaded Python 3Windows x86-64

libclang_ng-21.1.8.1-py3-none-manylinux_2_28_x86_64.whl (27.1 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ x86-64

libclang_ng-21.1.8.1-py3-none-manylinux_2_28_aarch64.whl (25.2 MB view details)

Uploaded Python 3manylinux: glibc 2.28+ ARM64

libclang_ng-21.1.8.1-py3-none-macosx_11_0_arm64.whl (28.2 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

libclang_ng-21.1.8.1-py3-none-macosx_10_9_x86_64.whl (29.3 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file libclang_ng-21.1.8.1-py3-none-win_arm64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 fa0e62bb0c78ccc9f4d9afbe0753242db732f23bef4470d6a09eac10e3d762a5
MD5 7574b41d7ff019d17523dfe47eddc632
BLAKE2b-256 20681ebb16cac8b4e24c8cb1f03d38677bca26e96b9cd7743cdd91e230fb8ef4

See more details on using hashes here.

File details

Details for the file libclang_ng-21.1.8.1-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 550bb2ac54f3d03095f7d2ff7c7255da9a1850849aa919729f9cf9e264bff6a0
MD5 fa666b287f529a662b5b3b8f5dfcc2f7
BLAKE2b-256 8c21945b23230c00eacccb6ac0ecbd01d1a30d9461940021e073d632edb2224c

See more details on using hashes here.

File details

Details for the file libclang_ng-21.1.8.1-py3-none-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9f0d08f60654b069d395ad737b46f8d6d5cbe41108879fc4bdde3516caa93c1b
MD5 83158750bd76a9d2f38190fa33e3a4cd
BLAKE2b-256 7393d3a65834312df2ca80c4ee504f7ce2e9fe83be19c7ec1cbf8fae62c4e69f

See more details on using hashes here.

File details

Details for the file libclang_ng-21.1.8.1-py3-none-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 9d98e6f871e49cd722c4767c4e457cdd3c6834bf3abf6160c4d2fb0f4304fa77
MD5 0fccd5be9b0e15c6fe2f43c39095c93e
BLAKE2b-256 2db133d495a885198d013e0cb0965b70953763e6800a814baac1baa09bcd1016

See more details on using hashes here.

File details

Details for the file libclang_ng-21.1.8.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e4491e164837f000c11843dbb420466b59564ba63404084a5aba63bb978aeeb7
MD5 2d4a4bc25dfc6b25b4cdca980e4e147c
BLAKE2b-256 3d4de3196f2c5d496baabe3934787b7f220e6e029f03f1fbd3eacdf858e18419

See more details on using hashes here.

File details

Details for the file libclang_ng-21.1.8.1-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for libclang_ng-21.1.8.1-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5ac20d379f3c90c571b830c08599d4fa39e846201df9300c0f8ff1aeb14e9c4a
MD5 8fc4f1c34ed76ce6c32a486fa8aceaf2
BLAKE2b-256 73214abcd0a7181fa9e1e3b8b3d8a4ca7eb6151e56da265eeeb8e98cb15950c9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page