Install & Compatibility
Where this runs
tested against v0.23.5 · 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
py 3.9
✕ build_error
✕ build_error
22MB installed
● package 22MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
language
✓ import tree_sitter_c_sharp
from tree_sitter import Language, Parser
✗ from tree_sitter.languages import csharp_grammar
The C# grammar is exposed directly from the `tree_sitter_c_sharp` package, not a submodule of `tree_sitter.languages`.
Language
✓ from tree_sitter import Language
Parser
✓ from tree_sitter import Parser
This quickstart demonstrates how to load the C# grammar provided by `tree-sitter-c-sharp`, create a `tree_sitter.Parser`, and parse a simple C# code snippet into a syntax tree. It then accesses basic information from the root node.
import tree_sitter_c_sharp
from tree_sitter import Language, Parser
# Load the C# grammar from the installed package
C_SHARP_LANGUAGE = Language(tree_sitter_c_sharp.language())
# Create a parser instance
parser = Parser()
parser.set_language(C_SHARP_LANGUAGE)
# C# source code to parse
code = """
using System;
namespace MyNamespace
{
class MyClass
{
static void Main(string[] args)
{
Console.WriteLine("Hello, Tree-sitter C#!");
}
}
}
"""
# Parse the code
tree = parser.parse(bytes(code, "utf8"))
# Print a snippet of the AST
root_node = tree.root_node
print(f"Root node type: {root_node.type}")
print(f"Number of children: {len(root_node.children)}")
if root_node.children:
first_child = root_node.children[0]
print(f"First child type: {first_child.type}")
print(f"First child text: {first_child.text.decode('utf8')}")
Debug
Known issues
gotchaThe `tree-sitter` Python library (PyPI package `tree-sitter`) is a required dependency but is NOT automatically installed with `tree-sitter-c-sharp`. You must install both separately.fixEnsure both `pip install tree-sitter` and `pip install tree-sitter-c-sharp` are executed.
affects: All versions
gotchaThe C# grammar evolves to support new language features (e.g., C# 10, C# 11). Older versions of `tree-sitter-c-sharp` might not correctly parse syntax from newer C# language versions, leading to `ERROR` nodes in the parse tree.fixAlways use the latest stable version of `tree-sitter-c-sharp` to ensure the broadest support for modern C# syntax. Regularly update the package for C# language compatibility.
affects: All versions, especially when using older grammar versions with newer C# code.
gotchaWhen working with `tree-sitter`, always pass byte strings (e.g., `bytes(code, 'utf8')`) to the parser's `parse` method. Passing a regular Python string will raise a `TypeError`.fixEncode your source code string to bytes, typically using UTF-8: `parser.parse(bytes(code_string, 'utf8'))`.
affects: All versions of `tree-sitter` library.
Upgrade
Version history
0.23.5latest on PyPI · released Apr 14, 2026
Audit
Dependencies
tree-sitterrequiredProvides the core `tree_sitter` Python bindings required to load and use the C# grammar.