Skip to main content

A lightweight method to call Rust code from Python

Project description

PyO2: A lightweight method to call Rust code from Python

Not affiliated at all with PyO3. Is that what you were looking for?

Usage

File: Cargo.toml

[package]
name = "mylib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
pyo2 = "0.1.0"

File: src/lib.rs

use pyo2::{PyStr, PyVec};

#[no_mangle]
pub extern "C" fn test(name: &PyStr, numbers: &mut PyVec<i64>) {
    println!("Hello, {}!", unsafe { name.as_str_unchecked() });
    println!("Sum of numbers: {}", numbers.iter().cloned().sum::<i64>());
    numbers[0] = 6;
}

File: test.py

from pyo2 import RustDLL

dll = RustDLL('./libmylib.so')

s = 'World'
lst = [1, 2, 3, 4, 5]
dll.test(s, lst)
print(lst)

Output:

Hello, World!
Sum of numbers: 15
[6, 2, 3, 4, 5]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyo2-0.1.3.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

pyo2-0.1.3-py3-none-any.whl (2.7 kB view details)

Uploaded Python 3

File details

Details for the file pyo2-0.1.3.tar.gz.

File metadata

  • Download URL: pyo2-0.1.3.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-41-generic

File hashes

Hashes for pyo2-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2edd3568ef2c0bcc8177b577a0ebc77f25bd07526a389fb88ef5dccaf1702d10
MD5 8838a57e2b14812afd9fd0972e88c822
BLAKE2b-256 5ae6d32cd0c5e6ec5a8097f60c0dd1654fbe35703356a63c26facce0fb10472c

See more details on using hashes here.

File details

Details for the file pyo2-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pyo2-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 2.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-41-generic

File hashes

Hashes for pyo2-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 479819145431ef0da75bfba711c470ae3d3cb9ff9faf41d27f0751c52b5a3c13
MD5 067a69afc4a1f9dd431869e88f8484c0
BLAKE2b-256 9e27ff84405799da089d0bc86b78e2b6c9cbc167c7c5b91c2473b55790fb8a34

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page