| Crates.io | python-mod |
| lib.rs | python-mod |
| version | 1.0.0 |
| created_at | 2023-10-27 08:17:16.201143+00 |
| updated_at | 2025-08-05 05:23:27.92814+00 |
| description | A macro library for including a Python module in Rust. At this point, very imcomplete. |
| homepage | |
| repository | https://github.com/rexlunae/python-mod-rs.git |
| max_upload_size | |
| id | 1015730 |
| size | 66,551 |
A Rust procedural macro library for embedding Python code (Rython) directly into Rust projects. This crate allows you to write Python functions and have them compiled to Rust at build time, providing seamless integration between Python-like syntax and Rust performance.
Rython is a limited subset of Python that can be compiled to Rust. This crate provides procedural macros that:
python-ast crate.py) and package-style modules (__init__.py)Note: Rython is currently a very limited subset of Python. This will hopefully expand over time as the ecosystem develops.
python_module! (with std) and python_module_nostd! (without std)Add this to your Cargo.toml:
[dependencies]
python-mod = "1.0.0"
Important: This crate requires nightly Rust due to the use of experimental procedural macro features.
rustup install nightly
rustup default nightly
Create a file src/math_ops.py:
# Simple math operations
def add(a, b):
return a + b
def multiply(x, y):
return x * y
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Constants
PI = 3.14159
In your src/main.rs or src/lib.rs:
use python_mod::python_module;
// Import the Python module
python_module!(math_ops);
fn main() {
// Use Python functions as if they were native Rust
let result = math_ops::add(5, 3);
println!("5 + 3 = {}", result);
let product = math_ops::multiply(4, 6);
println!("4 * 6 = {}", product);
let fib = math_ops::fibonacci(7);
println!("fibonacci(7) = {}", fib);
let pi = math_ops::PI;
println!("ฯ โ {}", pi);
}
cargo build
cargo run
use python_mod::python_module;
// Import a single Python file: src/utils.py
python_module!(utils);
fn example() {
let result = utils::some_function(42);
}
For a package-style module, create src/my_package/__init__.py:
def package_function():
return "Hello from package!"
def calculate_square(x):
return x * x
Then import it:
python_module!(my_package);
fn example() {
let message = my_package::package_function();
let square = my_package::calculate_square(5);
}
python_module!{advanced_module
// Rust code that will be included at the top of the generated module
use std::collections::HashMap;
use std::result::Result;
// This code becomes part of the generated module
type MyResult<T> = Result<T, String>;
}
fn example() {
// Now you can use both the Rust preamble and Python functions
let result = advanced_module::python_function();
}
use python_mod::python_module_nostd;
// Import without including standard Python libraries
python_module_nostd!(minimal_module);
fn example() {
let result = minimal_module::simple_function();
}
Currently, Rython supports a limited subset of Python syntax:
def)+, -, *, /)if, else)The macro looks for Python files in the following locations:
src/{module_name}.pysrc/{module_name}/__init__.pyExample project structure:
my_project/
โโโ Cargo.toml
โโโ src/
โ โโโ lib.rs
โ โโโ math_utils.py # Single file module
โ โโโ string_ops.py # Another single file module
โ โโโ advanced/ # Package module
โ โโโ __init__.py
Usage:
python_module!(math_utils); // Imports src/math_utils.py
python_module!(string_ops); // Imports src/string_ops.py
python_module!(advanced); // Imports src/advanced/__init__.py
python_module!(module_name)Imports a Python module with standard library support.
Parameters:
module_name: The name of the Python module to importExample:
python_module!(my_module);
python_module_nostd!(module_name)Imports a Python module without standard library support.
Parameters:
module_name: The name of the Python module to importExample:
python_module_nostd!(my_module);
python_module!{module_name /* Rust preamble */}Imports a Python module with custom Rust code preamble.
Parameters:
module_name: The name of the Python module to importExample:
python_module!{my_module
use std::collections::HashMap;
type MyError = String;
}
cargo build
The crate includes a comprehensive test suite with 23+ tests:
# Run all tests
cargo test
# Run specific test categories
cargo test --test lib_tests # Core functionality tests
cargo test --test documentation_tests # Documentation validation
cargo test --test edge_case_tests # Edge cases and boundaries
cargo testThe crate works by:
python-ast to parse Python into a Rust ASTTokenStreamPython Source โ python-ast โ Rust AST โ TokenStream โ Rust Module
Future improvements may include:
Licensed under the Apache License, Version 2.0. See LICENSE for details.
quote - For Rust code generationproc-macro2 - Advanced procedural macro functionalityproc-macro-error - Better error handling in macrospython-ast - Python AST parsing and Rust code generationpath_macro - Path manipulation utilitiesNote: This project is in active development. The Python subset supported by Rython is currently limited, but is expected to grow over time. Contributions and feedback are welcome!