Tree-sitter Python Bindings
raw JSON → 0.25.2 verified Tue May 12 auth: no python install: verified quickstart: stale
Tree-sitter provides Python bindings to the core Tree-sitter parsing library, enabling fast, incremental parsing and the generation of concrete syntax trees for various programming languages. It's actively maintained, with frequent updates (minor and patch releases typically every few weeks or months) to keep pace with the underlying C library. The current version is `0.25.2`.
pip install tree-sitter Common errors
error ModuleNotFoundError: No module named 'tree_sitter' ↓
cause The `tree-sitter` Python package is not installed in your current environment or is not accessible.
fix
pip install tree-sitter
error error: Microsoft Visual C++ 14.0 or greater is required. ↓
cause Building Tree-sitter language grammars or compiling the `py-tree-sitter` package requires a C/C++ compiler, which is missing on Windows.
fix
Install the "Build Tools for Visual Studio 2022" (or 2019) from Microsoft's website, ensuring to select the 'Desktop development with C++' workload during installation.
error FileNotFoundError: [Errno 2] No such file or directory: 'build/my-language.so' ↓
cause The compiled shared library for the Tree-sitter language grammar could not be found, likely because `tree_sitter.Language.build_library()` failed or the specified path is incorrect.
fix
Ensure
tree_sitter.Language.build_library() successfully compiles the grammar, verify the output_path matches where you later try to load it, and confirm the source_paths are correct. error AttributeError: 'Parser' object has no attribute 'set_language' ↓
cause You are attempting to use an outdated method (`set_language`) on the `tree_sitter.Parser` object; the API has changed.
fix
parser.language = language
Warnings
breaking The `Language` constructor no longer accepts a raw pointer (`int`) directly but expects a capsule object. If you were dynamically loading a `.so` file, you might need to use `ctypes.CDLL` and extract the language function or adjust your loading mechanism. Additionally, `Parser.parse()` no longer accepts a `keep_text` argument, and range arguments for `Query.captures()` and `Query.matches()` have been removed; use `query.set_byte_range()` or `query.set_point_range()` instead. ↓
fix For custom language loading, ensure you're passing a proper `PyCapsule` or using a language binding that provides it. Remove `keep_text` from `parser.parse()` calls. For queries, pre-set the desired range using `query.set_byte_range()` or `query.set_point_range()` before calling `captures()` or `matches()`.
breaking Python 3.9 is no longer supported; the library now requires Python 3.10 or newer. The `Language(ptr: int)` constructor was deprecated, urging users to use language binding packages instead of raw pointers. The method `Node.child_containing_descendant` was also deprecated. ↓
fix Upgrade your Python environment to 3.10 or later. Migrate away from `Language(ptr: int)` by using official language binding packages (e.g., `tree-sitter-python`) which provide a `language()` function. Update code using `Node.child_containing_descendant` to equivalent newer methods or tree traversal logic.
gotcha Incompatibilities can arise between the `tree-sitter` Python package and specific `tree-sitter-<language>` grammar packages if their underlying Tree-sitter ABI versions differ. This can lead to runtime errors when loading languages. ↓
fix Ensure that the `tree-sitter` package and any `tree-sitter-<language>` packages you use are compatible by checking their release notes or by installing versions known to work together. If encountering issues, try aligning their versions, typically by upgrading/downgrading one or both packages.
gotcha High memory usage has been reported in certain scenarios, particularly when parsing very large files or using complex grammars, potentially indicating memory leaks in specific contexts or substantial resource requirements of the core library. ↓
fix Monitor memory usage closely in production. For large inputs, consider processing files in chunks or optimizing your queries to reduce the scope. Report any persistent, unexplainable memory growth as a bug to the project maintainers, providing a minimal reproducible example.
gotcha While many languages have `tree-sitter-<language>` packages, for less common languages or custom grammars, you might need to manually compile the grammar into a shared library (`.so` or `.dll`) and then load it. The `tree-sitter-languages` package, which simplifies loading, is currently unmaintained and may have compatibility issues with newer `tree-sitter` versions. ↓
fix For unsupported languages, follow the official Tree-sitter documentation for compiling custom grammars. Then, use `from tree_sitter import Language; MY_LANGUAGE = Language(f'path/to/my_language.so', 'my_language_name')` to load it, ensuring the ABI version matches. Avoid `tree-sitter-languages` in new projects.
breaking The `tree_sitter.Parser` object reports an `AttributeError` indicating it lacks the `set_language` method. This method is expected to be present in `tree-sitter` version 0.23.2, suggesting a potential issue with the package installation, corruption, or an unexpected environment interaction preventing the method from being exposed. ↓
fix Verify the integrity of your `tree-sitter` installation by reinstalling it (`pip install --force-reinstall tree-sitter`). Ensure there are no conflicting packages or unusual environment configurations that might interfere with the package's functionality. If the issue persists, consider trying a different minor version of `tree-sitter` or consulting the project's issue tracker for known environment-specific problems.
breaking The `Parser.set_language()` method has been removed. ↓
fix Initialize the `Parser` with the `language` argument (e.g., `parser = Parser(language=MY_LANGUAGE)`) or assign the language directly to the `language` property (e.g., `parser.language = MY_LANGUAGE`).
Install
pip install tree-sitter-python Install compatibility verified last tested: 2026-05-12
python os / libc variant status wheel install import disk
3.10 alpine (musl) tree-sitter wheel - 0.01s 19.2M
3.10 alpine (musl) tree-sitter-python wheel - - 66.2M
3.10 alpine (musl) tree-sitter - - 0.01s 19.2M
3.10 alpine (musl) tree-sitter-python - - - -
3.10 slim (glibc) tree-sitter wheel 1.6s 0.00s 20M
3.10 slim (glibc) tree-sitter-python wheel 1.5s - 137M
3.10 slim (glibc) tree-sitter - - 0.00s 20M
3.10 slim (glibc) tree-sitter-python - - - -
3.11 alpine (musl) tree-sitter wheel - 0.02s 21.0M
3.11 alpine (musl) tree-sitter-python wheel - - 72.5M
3.11 alpine (musl) tree-sitter - - 0.02s 21.0M
3.11 alpine (musl) tree-sitter-python - - - -
3.11 slim (glibc) tree-sitter wheel 1.6s 0.02s 22M
3.11 slim (glibc) tree-sitter-python wheel 1.6s - 144M
3.11 slim (glibc) tree-sitter - - 0.01s 22M
3.11 slim (glibc) tree-sitter-python - - - -
3.12 alpine (musl) tree-sitter wheel - 0.01s 12.9M
3.12 alpine (musl) tree-sitter-python wheel - - 63.0M
3.12 alpine (musl) tree-sitter - - 0.01s 12.9M
3.12 alpine (musl) tree-sitter-python - - - -
3.12 slim (glibc) tree-sitter wheel 1.5s 0.01s 14M
3.12 slim (glibc) tree-sitter-python wheel 1.4s - 134M
3.12 slim (glibc) tree-sitter - - 0.01s 14M
3.12 slim (glibc) tree-sitter-python - - - -
3.13 alpine (musl) tree-sitter wheel - 0.01s 12.6M
3.13 alpine (musl) tree-sitter-python wheel - - 59.6M
3.13 alpine (musl) tree-sitter - - 0.02s 12.5M
3.13 alpine (musl) tree-sitter-python - - - -
3.13 slim (glibc) tree-sitter wheel 1.5s 0.01s 14M
3.13 slim (glibc) tree-sitter-python wheel 1.4s - 133M
3.13 slim (glibc) tree-sitter - - 0.01s 13M
3.13 slim (glibc) tree-sitter-python - - - -
3.9 alpine (musl) tree-sitter wheel - 0.01s 18.5M
3.9 alpine (musl) tree-sitter-python wheel - - 65.4M
3.9 alpine (musl) tree-sitter - - 0.01s 18.5M
3.9 alpine (musl) tree-sitter-python - - - -
3.9 slim (glibc) tree-sitter wheel 1.8s 0.00s 19M
3.9 slim (glibc) tree-sitter-python wheel 1.7s - 137M
3.9 slim (glibc) tree-sitter - - 0.01s 19M
3.9 slim (glibc) tree-sitter-python - - - -
Imports
- Language
from tree_sitter import Language - Parser
from tree_sitter import Parser - Query
from tree_sitter import Query - QueryCursor
from tree_sitter import QueryCursor - language wrong
from tree_sitter_python import language; PY_LANGUAGE = Language(language())correctimport tree_sitter_python as tspython; PY_LANGUAGE = Language(tspython.language())
Quickstart stale last tested: 2026-04-24
import os
from tree_sitter import Language, Parser
# NOTE: For this to work, you need 'tree-sitter-python' installed
# pip install tree-sitter-python
import tree_sitter_python as tspython
# Load the Python language grammar
PY_LANGUAGE = Language(tspython.language())
# Create a parser and set its language
parser = Parser()
parser.set_language(PY_LANGUAGE)
# Source code to parse
code = b"""
def greet(name): # type: (str) -> None
print(f"Hello, {name}!")
greet("World")
"""
# Parse the code
tree = parser.parse(code)
# Get the root node and print its type
root_node = tree.root_node
print(f"Root node type: {root_node.type}")
# Traverse the tree (example: find function definitions)
query_string = """
(function_definition
name: (identifier) @function.name)
"""
query = PY_LANGUAGE.query(query_string)
captures = query.captures(root_node)
for node, name in captures:
print(f"Found function: {node.text.decode('utf8')} (capture: {name})")