pyo3/guide/src/module.md

87 lines
2.8 KiB
Markdown
Raw Normal View History

# Python Modules
2018-05-19 15:27:26 +00:00
As shown in the Getting Started chapter, you can create a module as follows:
```rust
2019-02-01 13:01:18 +00:00
use pyo3::prelude::*;
// add bindings to the generated python module
// N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
2017-06-18 15:15:10 +00:00
/// This module is implemented in Rust.
2018-11-12 21:28:45 +00:00
#[pymodule]
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);
Ok(out)
}
Ok(())
}
// 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.
2018-11-12 21:25:45 +00:00
2019-11-25 19:34:29 +00:00
To import the module, either copy the shared library as described in [Get Started](./get_started.md) or use a tool, e.g. `maturin develop` with [maturin](https://github.com/PyO3/maturin) or `python setup.py develop` with [setuptools-rust](https://github.com/PyO3/setuptools-rust).
2018-11-12 21:25:45 +00:00
## Documentation
2017-06-18 15:15:10 +00:00
The [Rust doc comments](https://doc.rust-lang.org/stable/book/first-edition/comments.html) of the module initialization function will be applied automatically as the Python doc string of your module.
```python
import rust2py
print(rust2py.__doc__)
```
Which means that the above Python code will print `This module is implemented in Rust.`.
2018-11-12 21:25:45 +00:00
## 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:
2018-11-12 21:25:45 +00:00
```rust
2019-02-14 09:41:52 +00:00
use pyo3::prelude::*;
use pyo3::{wrap_pyfunction, wrap_pymodule};
2019-03-22 13:07:33 +00:00
use pyo3::types::IntoPyDict;
2019-02-14 09:41:52 +00:00
2018-11-12 21:25:45 +00:00
#[pyfunction]
fn subfunction() -> String {
"Subfunction".to_string()
}
#[pymodule]
fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {
2019-02-01 13:01:18 +00:00
module.add_wrapped(wrap_pyfunction!(subfunction))?;
2018-11-12 21:25:45 +00:00
Ok(())
}
#[pymodule]
fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {
2019-02-01 13:01:18 +00:00
module.add_wrapped(wrap_pymodule!(submodule))?;
2018-11-12 21:25:45 +00:00
Ok(())
}
fn nested_call() {
let gil = GILGuard::acquire();
let py = gil.python();
2019-02-01 13:01:18 +00:00
let supermodule = wrap_pymodule!(supermodule)(py);
let ctx = [("supermodule", supermodule)].into_py_dict(py);
2018-11-12 21:25:45 +00:00
2019-03-22 13:07:33 +00:00
py.run("assert supermodule.submodule.subfunction() == 'Subfunction'", None, Some(&ctx)).unwrap();
2018-11-12 21:25:45 +00:00
}
```
This way, you can create a module hierarchy within a single extension module.