Python Modules

As shown in the Getting Started chapter, you can create a module as follows:

use pyo3::prelude::*;

// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
/// This module is implemented in Rust.
fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {

    // PyO3 aware function. All of our python interfaces could be declared in a separate module.
    // Note that the `#[pyfn()]` annotation automatically converts the arguments from
    // Python objects to Rust values; and the Rust return value back into a Python object.
    #[pyfn(m, "sum_as_string")]
    fn sum_as_string_py(_py: Python, a:i64, b:i64) -> PyResult<String> {
       let out = sum_as_string(a, b);


// logic implemented as a normal rust function
fn sum_as_string(a:i64, b:i64) -> String {
    format!("{}", a + b).to_string()

# fn main() {}

The #[pymodule] procedural macro attribute takes care of exporting the initialization function of your module to Python. It can take as an argument the name of your module, which must be the name of the .so or .pyd file; the default is the Rust function's name.

To import the module, either copy the shared library as described in Get Started or use a tool, e.g. maturin develop with maturin or python develop with setuptools-rust.


The Rust doc comments of the module initialization function will be applied automatically as the Python doc string of your module.

import rust2py


Which means that the above Python code will print This module is implemented in Rust..

Modules as objects

In Python, modules are first class objects. This means that you can store them as values or add them to dicts or other modules:

# #![allow(unused_variables)]
#fn main() {
use pyo3::prelude::*;
use pyo3::{wrap_pyfunction, wrap_pymodule};
use pyo3::types::IntoPyDict;

fn subfunction() -> String {

fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {

fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {

fn nested_call() {
    let gil = GILGuard::acquire();
    let py = gil.python();
    let supermodule = wrap_pymodule!(supermodule)(py);
    let ctx = [("supermodule", supermodule)].into_py_dict(py);"assert supermodule.submodule.subfunction() == 'Subfunction'", None, Some(&ctx)).unwrap();

This way, you can create a module hierarchy within a single extension module.