5. Annotations
A Python module is defined in Nerodia by annotating Lean modules with metadata via commands and attributes. The Nerodia compiler reads this metadata and uses it to generate a Python extension with the desired properties.
A Lean module must first use py_module to declare itself as a Python module
before any of the other annotations can be used.
5.1. py_module
/-- My awesome Python module. -/
py_module "awesome"
Declares this Lean module to define a Python module with the specified name.
If a docstring is provided, it will be used as the __doc__ attribute
of the Python module (and will usually show up when an import of the module is
hovered in a Python editor).
5.2. @[py_module_fn]
/-- Formats the sum of two numbers as a string. -/
@[py_module_fn "sum_as_string"]
def sumAsString (a b : Nat) : String :=
toString (a + b)
Exports a Lean definition as a Python module function.
A different name can be specified for the Python definition via
@[py_module_fn "name"]. A common use case for this is to change casing.
Lean names are usually camel case and Python names snake case.
If the Lean definition has a docstring, it will be used as the __doc__
attribute of the Python function (and usually show up when the function is
hovered in a Python editor).
5.3. @[py_module_attr]
/-- A magic number for the module. -/
@[py_module_attr] def magic := 42
Exports a Lean definition as a Python module attribute. The value of the attribute is computed upon first import.
A different name can be specified for the Python definition via
@[py_module_attr "name"]. A common use case for this is to change casing.
Lean names are usually camel case and Python names snake case.
If the Lean definition has a docstring, it will be attached to the Python attribute (and usually show up when it is hovered in a Python editor).
5.4. @[py_module_init]
@[py_module_init]
def winOnlyAttr : PyModuleInit := .ofPyIO fun mod => do
if System.Platform.isWindows then
mod.addByString "isWindows" (← getPyTrue)
Registers a Lean definition as a Python module initializer.
The definition must have type PyModuleInit.
Module initializers are run during module initialization in the order they
appear in the Lean module.