Skip to content

badprog/badprog-rust-binding-python-with-pyo3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Explanation and tutorial:

https://www.badprog.com/rust-python-binding-setting-up-pyo3-crate-with-maturin

Summary

To generate the Rust binding library we're going to use Maturin.
It's a tool used to manage the compilation and the path where the dynamic library will be generated.

Before going further, let's create a virtual environment for Python:

python -m venv .venv1

Then, let's activate it:

. .venv1/bin/activate

With our Python setup ready, let's install the Maturin Python tool:

pip install maturin

Use it to generate the dynamic library then allowing Python to access it:

maturin develop -m classic_operations/Cargo.toml

Note here that we need to explicitely specify where is the Cargo.toml file to load because we use subprojects (potentially many Cargo.toml files).

It's also possible to go to the subproject directly and type the command without specifying the path of Cargo.toml (just "maturin develop").

It's now time to check with our Python file if our library really works.

python binding.py

You should see this output:

Total add: 213.0  
Total sub: -167.0  
Total div: 0.45  
Total mul: 1320.0

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published