Expand description
§Rustpy - Seamless Python Integration for Rust
Rustpy provides a simple and intuitive interface for embedding Python code in Rust, with a focus on Machine Learning workflows. Built on top of PyO3, it offers a lower learning curve while maintaining type safety.
§Features
- Easy Python code execution with the
python!macro - Type-safe conversions between Rust and Python types
- Support for ML libraries (NumPy, PyTorch, TensorFlow, etc.)
- Comprehensive error handling
- Global variable management
- Seamless integration with Rust’s type system
§Quick Start
use rustpy_ml::prelude::*;
fn main() -> rustpy_ml::Result<()> {
// Initialize the Python runtime
rustpy_ml::init()?;
// Execute Python code
let result: i32 = python!(-> i32, "1 + 1")?;
println!("Result: {}", result);
// Use ML libraries
py_exec!("
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(f'Array: {arr}')
print(f'Mean: {arr.mean()}')
")?;
Ok(())
}Modules§
- macro_
embed - prelude
- Prelude module for convenient imports
Macros§
- py_eval
- Macro to evaluate Python expression with type inference Usage: let result: i32 = py_eval!(“1 + 1”);
- py_exec
- Macro to execute Python code without returning a value Usage: py_exec!(“import numpy as np\nprint(np.version)”)
- py_
locals - Helper macro to create a locals HashMap Usage: py_locals!{“x” => 5, “y” => 10}
- python
- Simple macro to run Python code Usage: python!(“print(‘Hello from Python’)”)
- wrap_
pyfunction - Wraps a Rust function annotated with
#[pyfunction]. - wrap_
pyfunction_ bound Deprecated - Wraps a Rust function annotated with
#[pyfunction].
Structs§
- Borrowed
- A borrowed equivalent to
Bound. - Bound
- A GIL-attached equivalent to
Py<T>. - Py
- A GIL-independent reference to an object allocated on the Python heap.
- PyAny
- Represents any Python object.
- PyBool
- Represents a Python
bool. - PyClass
Initializer - Initializer for our
#[pyclass]system. - PyDict
- Represents a Python
dict. - PyErr
- Represents a Python exception.
- PyFloat
- Represents a Python
floatobject. - PyInt
- Represents a Python
intobject. - PyList
- Represents a Python
list. - PyModule
- Represents a Python
moduleobject. - PyRef
- A wrapper type for an immutably borrowed value from a
Bound<'py, T>. - PyRef
Mut - A wrapper type for a mutably borrowed value from a
Bound<'py, T>. - PyString
- Represents a Python
string(a Unicode string object). - PyTuple
- Represents a Python
tupleobject. - Python
- A marker token that represents holding the GIL.
Enums§
- Error
- Main error type for Rustpy
Traits§
- From
PyObject - Extract a type from a Python object.
- From
Python - Trait for converting Python objects to Rust types
- IntoPy
Deprecated - Defines a conversion from a Rust type to a Python object.
- Into
PyObject - Defines a conversion from a Rust type to a Python object, which may fail.
- Into
Python - Trait for converting Rust types to Python objects
- PyAny
Methods - This trait represents the Python APIs which are usable on all Python objects.
- PyBool
Methods - Implementation of functionality for
PyBool. - PyByte
Array Methods - Implementation of functionality for
PyByteArray. - PyBytes
Methods - Implementation of functionality for
PyBytes. - PyCapsule
Methods - Implementation of functionality for
PyCapsule. - PyComplex
Methods - Implementation of functionality for
PyComplex. - PyDict
Methods - Implementation of functionality for
PyDict. - PyFloat
Methods - Implementation of functionality for
PyFloat. - PyFrozen
SetMethods - Implementation of functionality for
PyFrozenSet. - PyList
Methods - Implementation of functionality for
PyList. - PyMapping
Methods - Implementation of functionality for
PyMapping. - PyMapping
Proxy Methods - Implementation of functionality for
PyMappingProxy. - PyModule
Methods - Implementation of functionality for
PyModule. - PySequence
Methods - Implementation of functionality for
PySequence. - PySet
Methods - Implementation of functionality for
PySet. - PySlice
Methods - Implementation of functionality for
PySlice. - PyString
Methods - Implementation of functionality for
PyString. - PyTraceback
Methods - Implementation of functionality for
PyTraceback. - PyTuple
Methods - Implementation of functionality for
PyTuple. - PyType
Methods - Implementation of functionality for
PyType. - PyWeakref
Methods - Implementation of functionality for
PyWeakref. - ToPy
Object Deprecated - Conversion trait that allows various objects to be converted into
PyObject.
Functions§
- call
- Call a Python function by module and function name
- clear_
globals - Clear all global variables
- eval
- Evaluate Python expression and return the result
- eval_
with_ locals - Evaluate Python expression with local variables
- exec
- Execute Python code and return the result
- exec_
with_ locals - Execute Python code with local variables
- from_
python - Helper function to convert Python object to Rust value
- get_
global - Get a global Python variable
- import
- Import a Python module
- init
- Initialize the Python runtime with optional ML library support This should be called before using any Python functionality
- init_
with_ ml - Initialize with specific ML libraries
- set_
global - Store a global Python variable
- to_
python - Helper function to convert Rust value to Python
Type Aliases§
- PyObject
- A commonly-used alias for
Py<PyAny>. - PyResult
- Represents the result of a Python call.
- Result
- Result type alias for Rustpy operations
Attribute Macros§
- pyclass
- pyfunction
- A proc macro used to expose Rust functions to Python.
- pymethods
- A proc macro used to expose methods to Python.
- pymodule
- A proc macro used to implement Python modules.