PyO3 examples

Overview

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.

You might also like...
PyO3's PyAny as a serde data format

serde-pyobject PyO3's PyAny as a serde data format Usage Serialize T: Serialize into &'py PyAny: use serde::Serialize; use pyo3::{Python, types::{PyAn

Schemars is a high-performance Python serialization library, leveraging Rust and PyO3 for efficient handling of complex objects

Schemars Introduction Schemars is a Python package, written in Rust and leveraging PyO3, designed for efficient and flexible serialization of Python c

Rust library for program synthesis of string transformations from input-output examples 🔮

Synox implements program synthesis of string transformations from input-output examples. Perhaps the most well-known use of string program synthesis in end-user programs is the Flash Fill feature in Excel. These string transformations are learned from input-output examples.

Examples of interacting with a Polkadot node using Rust

Examples of interacting with a Polkadot node Some examples of using JSON RPC to interact with a Polkadot node, working up to manually building and sub

Game examples implemented in rust console applications primarily for educational purposes.

rust-console-games A collection of game examples implemented as rust console applications primarily for providing education and inspiration. :) Game *

Examples on how to write Windows kernel drivers in Rust

windows-kernel-rs Note: this is still work in progress! This is a Windows kernel framework in Rust that consists of windows-kernel-sys, a crate that p

Examples and helpers to build NFT contracts on CosmWasm

CosmWasm NFTS This repo is the official repository to work on all NFT standard and examples in the CosmWasm ecosystem. cw721 and cw721-base were moved

Code examples for https://www.poor.dev/blog/terminal-anatomy/

This repository contains examples from the Anatomy of a Terminal Emulator blog post. Each folder contains a separate example and can be run separately

Examples of Solana on-chain programs

spl-examples List of Solana on-chain programs which demonstrate different aspects of Solana architecture. 01__state It's a counter program. Each user

zine/book about bitmap drawing algorithms and math with code examples in Rust
zine/book about bitmap drawing algorithms and math with code examples in Rust

A Bitmapper's Companion - zine/book about bitmap drawing algorithms and math with code examples in Rust A small zine/book written in LaTeX. In progres

Simple examples to demonstrate full-stack Rust audio plugin dev with baseplug and iced_audio
Simple examples to demonstrate full-stack Rust audio plugin dev with baseplug and iced_audio

iced baseplug examples Simple examples to demonstrate full-stack Rust audio plugin dev with baseplug and iced_audio WIP (The GUI knobs do nothing curr

Examples to demonstrate how to use PipeCD

examples A repository contains some examples for PipeCD. NOTE: This repository is automatically synced from the examples directory of pipe-cd/pipe rep

Examples for Deno Deploy

Deno Deploy Examples This repository contains a list of examples for Deno Deploy. fetch - Make outbound requests using the fetch() API. json_html - Re

Community showcase and examples of Actix ecosystem usage.

Actix Examples Curated examples using the Actix ecosystem. Community Showcase Merino: Web service for Firefox Suggest lemmy: A federated alternative t

Examples of cw20 usage, extracted from cw-plus, maintained by the community

CosmWasm Tokens This is a collection of cw20-related contracts extracted from cw-plus. These serve as examples of what is possible to build and as sta

Examples from the talk Why you should learn Rust

Why you should learn Rust: Examples This repository contains the examples from the talk "Why you should learn Rust". All examples demonstrate that the

A pretty, sensible logger for Rust - ideal for running examples and tests on a crate of choice
A pretty, sensible logger for Rust - ideal for running examples and tests on a crate of choice

sensible-env-logger A pretty, sensible logger for Rust - ideal for running examples and tests on a crate of choice. This is a thin wrapper around pret

A simple, modern fuzzy finder tool to run examples in a Cargo project.

cargo-rx cargo-rx is a simple, modern Runner for Examples in a Cargo project. This crate provides a single executable: rx. Basically anywhere you woul

A collection (eventually) of examples that use some non-beginner things.

nannou examples A collection (eventually) of examples that use some non-beginner things. Right now the only example combines nannou's standard draw AP

Comments
  • ModuleNotFoundError: No module named 'rust'

    ModuleNotFoundError: No module named 'rust'

    Thanks for the nice tutorial. I was able to follow most of the steps, but it seems the system built multiply without rust? Tried poking around the .env folder and didn't find the rust package there, either, but perhaps I missed it. Any suggestions would be much appreciated. Thanks!

    (base) COMPUTERNAME:~ user$ cd /py03/learning/pyo3/multiply
    (base) COMPUTERNAME:multiply user$ python3 -m venv .env
    (base) COMPUTERNAME:multiply user$ source .env/bin/activate
    (.env) (base) COMPUTERNAME:multiply user$ pip install maturin
    (.env) (base) COMPUTERNAME:multiply user$ maturin develop
    🔗 Found pyo3 bindings
    🐍 Found CPython 3.9 at /opt/anaconda3/bin/python
        Finished dev [unoptimized + debuginfo] target(s) in 0.58s
    📦 Built wheel for CPython 3.9 to /var/folders/dh/2hskntz961l2r01cdbd45pfxp0_36_/T/.tmpiyS39m/multiply-0.1.0-cp39-cp39-macosx_10_7_x86_64.whl
    🛠  Installed multiply-0.1.0
    (.env) (base) COMPUTERNAME:multiply user$ python3 multiply.py
    Traceback (most recent call last):
      File "./pyo3/multiply/multiply.py", line 1, in <module>
        import rust
    ModuleNotFoundError: No module named 'rust'
    
    opened by ghproek 2
Owner
Words
null
Community showcase and examples of Actix ecosystem usage.

Actix Examples Curated examples using the Actix ecosystem. Community Showcase Merino: Web service for Firefox Suggest lemmy: A federated alternative t

Actix 2.9k Dec 31, 2022
Examples from the talk Why you should learn Rust

Why you should learn Rust: Examples This repository contains the examples from the talk "Why you should learn Rust". All examples demonstrate that the

Björn Lange 1 Aug 12, 2022
A repository of examples for esp32s3-box written in Rust

ESP32S3-BOX Examples A repository of examples for esp32s3-box written in Rust Usage hello world cargo espflash --release --monitor esp demo animati

null 8 Dec 19, 2022
Winsafe-examples - Examples of native Windows applications written in Rust with WinSafe.

WinSafe examples This repo contains several examples of native Win32 applications written in Rust with WinSafe. All examples follow the same program s

Rodrigo 40 Dec 14, 2022
Msgpack serialization/deserialization library for Python, written in Rust using PyO3, and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Aviram Hassan 139 Dec 30, 2022
PyO3-based Rust binding of NumPy C-API

rust-numpy Rust bindings for the NumPy C-API API documentation Latest release (possibly broken) Current Master Requirements Rust >= 1.41.1 Basically,

PyO3 759 Jan 3, 2023
Pyo3 - Rust bindings for the Python interpreter

PyO3 Rust bindings for Python, including tools for creating native Python extension modules. Running and interacting with Python code from a Rust bina

PyO3 7.2k Jan 2, 2023
Python bindings for akinator-rs using pyo3

Akinator-py python bindings for akinator-rs using pyo3 Installation Prebuilt wheels are uploaded onto pypi, if you platform is supported, you can inst

Tom-the-Bomb 4 Nov 17, 2022
PyO3 bindings and Python interface to skani, a method for fast fast genomic identity calculation using sparse chaining.

?? ⛓️ ?? Pyskani PyO3 bindings and Python interface to skani, a method for fast fast genomic identity calculation using sparse chaining. ??️ Overview

Martin Larralde 13 Mar 21, 2023
Sample Python extension using Rust/PyO3/tch to interact with PyTorch

Python extensions using tch to interact with PyTorch This sample crate shows how to use tch to write a Python extension that manipulates PyTorch tenso

Laurent Mazare 5 Jun 10, 2023