-
The documentation doesn't seem to be clear on that. Context: what I'd like to do is to import this code into my Rust Drops of Diamond project for blob serialization with building sharding with Ethereum. It would be nice if I could copy and paste a Python file into a Rust file, and make a few modifications to make it compatible with Rust. At the moment the documentation doesn't seem to be clear on how to do that. It seems to talk about using Rust to make a Python compatible library, but not using Python in Rust. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hi @jamesray1, I think this is feasible (although not documented at all) by doing the following: To embed the Python file within Rust:
To use an external Python file:
Then you can have access to your function and call it with an argument that implements With an external file, the code looks like so: extern crate pyo3;
use pyo3::prelude::*;
fn main() {
let gil = Python::acquire_gil();
let py = gil.python();
let syspath: &PyList = py.import("sys")
.unwrap()
.get("path")
.unwrap()
.try_into()
.unwrap();
syspath.insert(0, "path/to/your/module").unwrap();
let evm_utils = py.import("evm.utils").unwrap();
evm_utils.call1("serialize_blobs", ...);
} |
Beta Was this translation helpful? Give feedback.
-
I have tried to make importing work with different packages inside the Rust code with the example above. But I wasn't successful. Can you point me out how do you import external python code? import numpy as np
def test(args):
return args Code above throws
|
Beta Was this translation helpful? Give feedback.
-
Trying to import a python file into rust code inorder to convert server side code from Flask to rocket ,Is it really possible..??!! |
Beta Was this translation helpful? Give feedback.
Hi @jamesray1,
I think this is feasible (although not documented at all) by doing the following:
To embed the Python file within Rust:
include_str!
to embed your Python source into your compiled executable / librarypyo3::Python.eval
to add all your function definitions to the localsTo use an external Python file:
py.import("sys")?.get("path")
)(you can also simply edit
sys.path
within Rust to import the required Python library, but from that will work only if you have installed the Python script to a particular location)eval
to define your function within the interpreterThen you can have access to your function and…