{"id":3281,"library":"starlark-pyo3","title":"Starlark-pyo3","description":"Starlark-pyo3 is a Python wrapper for the `starlark-rust` interpreter, bringing Google's Starlark language (a dialect of Python used in Bazel) to Python applications. It enables executing Starlark scripts, evaluating expressions, and integrating with Python objects. The library is currently at version 2025.2.5 and follows a rapid release cadence, often tied to updates in its underlying Rust dependencies.","status":"active","version":"2025.2.5","language":"en","source_language":"en","source_url":"https://github.com/inducer/starlark-pyo3","tags":["starlark","rust","interpreter","configuration","pyo3","bazel"],"install":[{"cmd":"pip install starlark-pyo3","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Starlark","correct":"from starlark_pyo3 import Starlark"},{"note":"Required to initialize the Rust Starlark interpreter, typically called once at application startup.","symbol":"init_once","correct":"from starlark_pyo3 import init_once"}],"quickstart":{"code":"from starlark_pyo3 import Starlark, init_once\nimport os\n\n# Initialize the Starlark interpreter (safe to call multiple times)\ninit_once()\n\n# Define a simple Starlark script\nstarlark_script = '''\nload('foo.star', 'my_func')\ndef greet(name):\n    return f\"Hello, {name}!\"\n\nmy_value = greet(\"Starlark\")\n'''\n\n# Define a load function for external Starlark files\ndef load_func(name):\n    if name == 'foo.star':\n        return \"def my_func(x): return x * 2\"\n    raise ValueError(f\"Unknown module: {name}\")\n\n# Create a Starlark instance with custom globals and a load function\nstarlark_interpreter = Starlark(globals={'MAGIC_NUMBER': 42}, load_func=load_func)\n\n# Evaluate the script\nresult_globals = starlark_interpreter.eval(starlark_script)\n\n# Access a variable from the evaluated script\nprint(f\"Result from Starlark: {result_globals['my_value']}\")\n\n# Call a function directly from the evaluated module\nprint(f\"Calling greet: {result_globals['greet']('World')}\")\n\n# Verify a global was passed\nprint(f\"Magic number: {result_globals['MAGIC_NUMBER']}\")\n\n# Evaluate an expression directly\nexpression_result = starlark_interpreter.eval('10 * MAGIC_NUMBER')\nprint(f\"Expression result: {expression_result}\")","lang":"python","description":"This quickstart demonstrates how to initialize the Starlark interpreter, define a load function for importing other Starlark files, evaluate a Starlark script, and access global variables and functions from the evaluated context. It also shows how to pass Python objects as globals into the Starlark environment."},"warnings":[{"fix":"Thoroughly test your application after upgrading. Consult the changelog for `starlark-pyo3` and the upstream `starlark-rust` and `pyo3` libraries for specific API changes. Re-evaluate any custom Python objects passed into Starlark.","message":"Major version upgrades of `pyo3` and `starlark-rust` can introduce breaking changes in API behavior or underlying data structures, especially if you're interacting with lower-level components or custom extensions. Always review the release notes carefully.","severity":"breaking","affected_versions":"v2025.2, v2025.1, v2024.1.5 (and potentially others)"},{"fix":"Ensure `from starlark_pyo3 import init_once` and `init_once()` are present in your application's bootstrap code, typically before any `Starlark` instances are created or `eval` operations are performed.","message":"The `init_once()` function must be called to initialize the underlying Rust Starlark interpreter. While it's safe to call multiple times, forgetting it will lead to errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review release notes for changes related to type checking and Python object handling. Validate existing Starlark scripts against new versions, paying attention to type coercion and object visibility.","message":"Changes to Starlark's type system or evaluation rules (e.g., `typecheck` introduction, handling of Python objects) might subtly alter script behavior or introduce new errors.","severity":"gotcha","affected_versions":"v2025.2, v2025.2.5"},{"fix":"Update your code to leverage the direct `kwargs` support when calling Starlark functions from Python. Remove any manual workarounds for keyword arguments.","message":"The `FrozenModule.call` method gained `kwargs` support in `v2025.2.3`. If you previously had workarounds for passing keyword arguments or expected specific behavior, this change might impact your code.","severity":"gotcha","affected_versions":"Prior to v2025.2.3"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}