Skip to content

saidvandeklundert/pyo3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyO3 examples

This repo accompanies this blog post.

The Rust examples, found here, include:

  • calculate the n-th Fibonacci in Python as well as in Rust
  • having Python use a variety of types in Rust functions
  • using a Rust struct in Python code
  • using Python to send JSON to Rust and serialize that JSON as a struct
  • allow Rust to use the logger from the Python runtime
  • generating an Error in Rust and catching it as an exception in Python

The Rust code is being called from a Python script found here.

To play with the code:

git clone https://github.com/saidvandeklundert/pyo3.git
cd pyo3
docker build ./ -t pyo3
docker run --name='pyo3' --hostname='pyo3' -di pyo3:latest
docker exec -it pyo3 bash
cd /opt/pyo3/pyo3
python3 -m venv /opt/venv
. /opt/venv/bin/activate
maturin develop --release
python example.py

After updating the code, you can do the following to work with the updates that you made:

git pull
python3  -m venv .env
source .env/bin/activate
maturin develop --release

More information

For more information, check the PyO3 repository here or check the PyO3 user guide here.