An embeddable scripting language for Python

Let your users script your Python application. Koskript parses and runs small scripts inside a KoskriptRuntime, and any Python value you expose becomes available to them.

Python 3.10+ · MPL-2.0 · install via pip

Install

bash
pip install -U koskript

The default parser ships with lark as a dependency. Prefer zero dependencies? Use the experimental native parser: KoskriptRuntime(native_parser_experiment=True).

Run a script

python
from koskript import KoskriptRuntime

runtime = KoskriptRuntime()
runtime.execute("""
const names = ["Ana", "Luis"]
array.each(names, (name) { print("hello " + name) })
""")
output
hello Ana
hello Luis

Expose your own API

Any Python callable works out of the box — no wrappers to write by hand:

python
runtime = KoskriptRuntime({"double": lambda x: x * 2})
runtime.execute("return double(21)")  # 42

What the language looks like

koskript
class Counter {
    private count = 0
    constructor() { this.count = 1 }
    public fn bump() {
        this.count = this.count + 1
        return this.count
    }
}

const c = new Counter()
print(c.bump() + c.bump())  # 5

error HttpError (err, status) {
    err.message = "request failed"
    err.status = status
}

try {
    throw HttpError(404)
} catch e {
    print(e.status)  # 404
}

What it gives you

Scoping & closures

local / const per block; lambdas capture their defining scope.

Functions & decorators

fn, lambdas, recursion, wrappers with @decorator.

Classes

Inheritance, visibility, static members, constructors, super.

Control flow

if / elseif / else; while, for, foreach.

Errors

error, throw, try / catch / finally.

Embedding

Modules, namespaces, stdlib (array, map, string, math, json…), Python interop.

Documentation

DocumentWhat it covers
Getting startedInstallation, first script, a complete example
Language referenceTypes, variables, operators, control flow, functions, modules, strings, bytes
ClassesInheritance, visibility, static members, constructors, super
Standard libraryCore builtins and the map, array, string, bytes, math, json namespaces
Python interopExposing Python values, callables and classes to scripts
Embedding guideKoskriptRuntime, run(), error handling, stdlib setup
RoadmapWhat is done, what is planned, known limitations

Koskript is in early development; the language and its API may change between releases. License: MPL-2.0.