Rustpy ๐ฆ๐
Seamless Python Integration for Rust with Focus on Machine Learning
Rustpy is a Rust library that provides a simple and intuitive interface for embedding Python code in Rust applications. Built on top of PyO3, it offers a lower learning curve while maintaining type safety and enabling seamless integration with Python's rich ML ecosystem.
๐ฏ Goals
- Easy Python Integration: Execute Python code directly in Rust with minimal boilerplate
- ML-Friendly: First-class support for NumPy, PyTorch, TensorFlow, and other ML libraries
- Type Safety: Leverage Rust's type system with automatic Python โ Rust conversions
- Low Learning Curve: Simple macros and intuitive API for Python developers transitioning to Rust
- Debugging Support: Comprehensive error handling with Python tracebacks
- Performance: Combine Python's ML ecosystem with Rust's performance and safety
๐ Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.1"
Basic Usage
use *;
๐ Features
1. Simple Macro-Based Interface
use *;
// Evaluate expressions with type inference
let x: i32 = python!?;
let y: f64 = python!?;
let s: String = python!?;
// Execute statements
py_exec!?;
// Work with collections
let numbers: = python!?;
2. Pass Rust Variables to Python
use *;
let x = 10;
let y = 20;
let locals = py_locals! ;
let result: i32 = python!?;
println!; // Output: Result: 30
3. Machine Learning Support
use *;
// Initialize with ML libraries
init_with_ml?;
// Use NumPy
py_exec!?;
// Get results back to Rust
let mean: f64 = python!?;
4. Data Processing Pipeline
use *;
// Generate data in Rust
let data: = .map.collect;
// Pass to Python for analysis
let locals = py_locals! ;
let stats: = python!?;
println!;
5. Comprehensive Error Handling
use *;
match python!
๐ Examples
Basic Example
ML with NumPy
Advanced Data Processing
๐ API Overview
Initialization
// Basic initialization
init?;
// Initialize with specific ML libraries
init_with_ml?;
Macros
python!(-> Type, "code")- Evaluate Python expression and return typed resultpy_exec!("code")- Execute Python statementspy_eval!("code")- Evaluate expression with type inferencepy_locals!{key => value}- Create locals HashMap for variable passing
Runtime Functions
// Execute Python code
exec?;
// Evaluate expressions
let result: i32 = eval?;
// Import modules
let numpy = import?;
// Call Python functions
let result: f64 = call?;
Type Conversions
Rustpy supports automatic conversion between Rust and Python types:
| Rust Type | Python Type |
|---|---|
i32, i64 |
int |
f32, f64 |
float |
bool |
bool |
String, &str |
str |
Vec<T> |
list |
HashMap<K, V> |
dict |
Option<T> |
None or value |
๐ง Advanced Usage
Custom Type Conversions
use *;
// Convert Point to Python dict
let point = Point ;
let locals = py_locals! ;
let distance: f64 = python!?;
Module Management
// Import and use modules
let json_module = import?;
py_exec!?;
Global Variables
// Store global variables
with_gil;
// Retrieve global variables
if let Some = get_global?
๐ฏ Use Cases
1. Python ML Scripts in Rust Applications
Perfect for embedding Python ML models in Rust backend services while maintaining type safety.
2. Rapid Prototyping
Use Python for quick experimentation and Rust for production code in the same project.
3. Data Science Workflows
Leverage Python's data science ecosystem (NumPy, Pandas, Matplotlib) with Rust's performance.
4. Gradual Migration
Migrate Python codebases to Rust incrementally while keeping critical Python components.
๐งช Testing
Run tests:
# Basic tests
# ML tests (requires NumPy)
๐ Requirements
- Rust 2021 edition or later
- Python 3.7+
- PyO3 compatible Python installation
For ML Features
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ License
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
๐ Acknowledgments
- Built on top of PyO3
- Inspired by the Python and Rust communities
- Special thanks to all contributors
๐ Links
๐ง Roadmap
- Async Python execution
- Better debugging tools
- PyTorch integration helpers
- TensorFlow integration helpers
- Jupyter notebook integration
- Performance benchmarks
- More examples and tutorials
Made with โค๏ธ for Python developers learning Rust and Rust developers using Python ML libraries.