Lython is an embeddable Python runtime for .NET 10 and .NET 11, implemented in managed C#. It supports a broad Python 3.13 subset for text, CSV, JSON and file-processing scripts. File, process and stream effects pass through an application-supplied host; scripts have no ambient machine access.
dotnet add package Lokad.Lythonusing Lokad.Lython;
// host implements ILythonHost; the application supplies its capabilities.
var engine = new LythonEngine();
var budget = new LythonMemoryBudget(16 * 1024 * 1024);
using var compiled = engine.Compile("""
import json
values = json.loads(payload)
return sum(values)
""", budget);
using var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(1));
using var result = await compiled.RunAsync(host, new LythonRunOptions
{
Globals = new Dictionary<string, object?> { ["payload"] = "[1,2,3]" },
AllowedBuiltinModules = new HashSet<string> { "json" },
DisableLocalModuleImports = true,
CancellationToken = cancellation.Token,
});
if (result.Success) Console.WriteLine(result.ReturnValue); // 6
else Console.WriteLine(result.Failure?.Message);Compiled programs can be reused. Dispose results after consuming their values, and await all executions before disposing compiled programs or their owners. Storage admission borrows the host budget; no budget means no implicit memory cap. Cancellation is cooperative, and the host must arrange finite deadlines. This managed runtime is an embedding boundary, not an OS isolation boundary.
Regular expressions require an explicitly supplied Python regex provider. The core has no Utf8Regex dependency. An optional adapter is kept in this repository, with documented cancellation and compatibility limits. It is not published as a NuGet package.
These guides describe the current repository implementation. For a published package, use the documentation from that package's source revision.
| Guide | Contents |
|---|---|
| Compatibility | Grammar inventory, Python semantics and explicit limits |
| Modules | Builtin module surface and specific boundaries |
| Runtime | Embedding, memory ownership, regex providers, host authority and cancellation |
| Benchmarks | CPython comparisons, boundaries and uncertainty |
| Testing | Focused regressions, platform checks and package qualification |
Release notes record API changes. Benchmark artifacts and
experimental history live under benchmarks/; plans and handoffs are outside
docs/.