Install & Compatibility
Where this runs
tested against v0.23.4 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 22.7MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.008s · 24MB
21MB installed
● package 21MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Language, Parser
✓ from tree_sitter import Language, Parser
✗ from tree_sitter_cpp import Language, Parser
The `Language` and `Parser` classes are part of the core `tree-sitter` library, not `tree-sitter-cpp` itself. `tree_sitter_cpp` provides the grammar object.
language()
✓ import tree_sitter_cpp as ts_cpp
cpp_language = Language(ts_cpp.language())
✗ cpp_language = Language.build_library('/path/to/grammar.so', ['/path/to/tree-sitter-cpp'])
For pre-compiled grammars installed via pip, the `language()` function from the grammar package (e.g., `tree_sitter_cpp.language()`) should be used directly with `tree_sitter.Language`. The `Language.build_library` method was removed from `py-tree-sitter` around version 0.21.x and is no longer the recommended approach for using distributed grammars.
This quickstart demonstrates how to load the `tree-sitter-cpp` grammar, initialize a `tree_sitter` parser with it, and parse a simple C++ code snippet. It then shows how to access the root node of the generated syntax tree and print basic information about it and its children. The code uses `bytes` objects for source code, as required by the `tree-sitter` library.
import tree_sitter_cpp as ts_cpp
from tree_sitter import Language, Parser
# Load the C++ grammar
CPP_LANGUAGE = Language(ts_cpp.language())
# Create a parser and set its language
parser = Parser()
parser.set_language(CPP_LANGUAGE)
# C++ code to parse
cpp_code = b"""#include <iostream>
int main() {
std::cout << "Hello, Tree-sitter!" << std::endl;
return 0;
}
"""
# Parse the code
tree = parser.parse(cpp_code)
# Get the root node of the syntax tree
root_node = tree.root_node
# Print the root node type and its first child (for demonstration)
print(f"Root node type: {root_node.type}")
if root_node.children:
print(f"First child type: {root_node.children[0].type}")
print(f"First child text: {root_node.children[0].text.decode('utf8')}")
# Example of traversing the tree (optional, for deeper analysis)
def traverse(node, depth=0):
print(' ' * depth + f'- {node.type} [ {node.start_point} - {node.end_point} ] {node.text.decode('utf8') if node.text else ''}')
for child in node.children:
traverse(child, depth + 1)
# print("\nFull AST:")
# traverse(root_node)
Debug
Known issues
breakingThe `Language.build_library` static method was removed from the `py-tree-sitter` library around version 0.21.x.fixInstead of `Language.build_library(...)`, you should `pip install` the specific grammar package (e.g., `tree-sitter-cpp`) and load it using `Language(grammar_package.language())`. For `tree-sitter-cpp`, this means `import tree_sitter_cpp as ts_cpp; cpp_language = Language(ts_cpp.language())`.
affects: tree-sitter>=0.21.x
gotchaInstalling `tree-sitter-cpp` alone is not sufficient to parse C++ code in Python. The core `tree-sitter` Python package, which provides the `Language` and `Parser` classes, must also be installed and imported.fixEnsure both packages are installed: `pip install tree-sitter tree-sitter-cpp`. Then, import `Language` and `Parser` from `tree_sitter` and the grammar from `tree_sitter_cpp`.
affects: All
gotchaThe `tree-sitter` parsing functions expect source code as a `bytes` object (UTF-8 or UTF-16 encoded), not a standard Python `str`.fixEncode your source strings before passing them to `parser.parse()`: `parser.parse(your_string.encode('utf8'))`. affects: All
Upgrade
Version history
0.23.4latest on PyPI · released Nov 11, 2024
Audit
Dependencies
tree-sitterrequiredProvides the core Python bindings and parsing engine required to utilize the C++ grammar. tree-sitter-cpp only provides the language definition.