pyo3::prelude

Attribute Macro pymodule

#[pymodule]
Expand description

A proc macro used to implement Python modules.

The name of the module will be taken from the function name, unless #[pyo3(name = "my_name")] is also annotated on the function to override the name. Important: the module name should match the lib.name setting in Cargo.toml, so that Python is able to import the module without needing a custom import loader.

Functions annotated with #[pymodule] can also be annotated with the following:

AnnotationDescription
#[pyo3(name = "...")]Defines the name of the module in Python.
#[pyo3(submodule)]Skips adding a PyInit_ FFI symbol to the compiled binary.
#[pyo3(module = "...")]Defines the Python dotted.path to the parent module for use in introspection.
#[pyo3(crate = "pyo3")]Defines the path to PyO3 to use code generated by the macro.

For more on creating Python modules see the module section of the guide.

Due to technical limitations on how #[pymodule] is implemented, a function marked #[pymodule] cannot have a module with the same name in the same scope. (The #[pymodule] implementation generates a hidden module with the same name containing metadata about the module, which is used by wrap_pymodule!).