A simple library to allow for easy use of python from rust.

Overview

Rustpy

A simple library to allow for easy use of python from rust.

Status

Currently this library has not received much love (pull requests welcome for any interested) and does not build with rust 1.0.

For another library that also strives to bridge the gap between python and rust and might be a little more up to day see:

https://github.com/dgrunwald/rust-python27-sys

https://github.com/dgrunwald/rust-cpython

How to Use

This library is meant to be middle ware for users wanting to use python libraries from rust. It allows users to quickly use existing tools and get working on interesting things fast!

See pysmtplib.rs for an example of how to bind enough smtplib to send emails.

For more documentation, run rustdoc src/rustpy.rs and look at doc/rustpy/index.html. Pull requests are welcome!

(untyped_res).unwrap(); assert_eq!(result, 12f32); }">
extern crate rustpy;
use rustpy::{ToPyType, FromPyType, PyState};

fn main() {
  let py = PyState::new();
  let module = py.get_module("math").unwrap();
  let func = module.get_func("sqrt").unwrap();
  let args = (144f32, ).to_py_object(&py).unwrap();
  let untyped_res = func.call(&args).unwrap();
  let result = py.from_py_object::<f32>(untyped_res).unwrap();
  assert_eq!(result, 12f32);
}

Important note: Only create one instance of PyState at a time. On construction, it grabs a global lock to prevent more than one thread from interacting with the interpreter thus making it very easy to deadlock.

Comments
  • Just my rust setup or something incompatible with latest nightly?

    Just my rust setup or something incompatible with latest nightly?

    src/rustpy.rs:38:1: 38:19 error: can't find crate for `sync`
    src/rustpy.rs:38 extern crate sync;
                     ^~~~~~~~~~~~~~~~~~
    error: aborting due to previous error
    Could not compile `rustpy`.
    
    opened by geekpete 5
  • fail -> panic

    fail -> panic

    Sorry I broke your code with rust-lang/rust#17894 ! Here's a fix :heart:

    (this is a semi-automatic PR, so sorry if it's not perfect. Let me know and I'll fix any problems.)

    opened by steveklabnik 3
  • error: found possibly newer version of crate `std` which `rustpy` depends on

    error: found possibly newer version of crate `std` which `rustpy` depends on

    Hi,

    Trying to build the example I get this error: /home/v/ue/rust/v_rustpy1/src/main.rs:1:1: 1:21 error: found possibly newer version of crate std which rustpy depends on

    I use the rust-nightly ppa on ubuntu 14.10 and the newest cargo. Thanks

    Compiling v_rustpy1 v0.0.1 (file:///home/v/ue/rust/v_rustpy1) /home/v/ue/rust/v_rustpy1/src/main.rs:1:1: 1:21 error: found possibly newer version of crate std which rustpy depends on /home/v/ue/rust/v_rustpy1/src/main.rs:1 extern crate rustpy; ^~~~~~~~~~~~~~~~~~~~ /home/v/ue/rust/v_rustpy1/src/main.rs:1:1: 1:21 note: perhaps this crate needs to be recompiled? /home/v/ue/rust/v_rustpy1/src/main.rs:1 extern crate rustpy; ^~~~~~~~~~~~~~~~~~~~ /home/v/ue/rust/v_rustpy1/src/main.rs:1:21: 1:21 note: crate std path #1: /usr/lib/rust/rust-nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.rlib /home/v/ue/rust/v_rustpy1/src/main.rs:1:21: 1:21 note: crate std path #2: /usr/lib/rust/rust-nightly/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-4e7c5e5c.so /home/v/ue/rust/v_rustpy1/src/main.rs:1:21: 1:21 note: crate rustpy path #1: /home/v/ue/rust/v_rustpy1/target/deps/librustpy-2037a4f507dfdb30.rlib error: aborting due to previous error Could not compile v_rustpy1.

    To learn more, run the command again with --verbose. [Finished in 0.3s with exit code 101] [cmd: ['cargo', 'run']] [dir: /home/v/ue/rust/v_rustpy1/src] [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

    opened by v217 2
  • how to get around global lock

    how to get around global lock

    hey - I have a situation where I need to run some Python code from Rust but I will have many Python processes running simultaneously. I am advanced at Python but a beginner in Rust, so I thought I would reach out regarding how to get around the global lock in this library. Would I need to edit the code so that a new state generates a new Python process and has its own lock? Why did you pick a global lock in the first place (so I understand the hurdles to what I am planning)?

    opened by jonathanstrong 1
  • Split PyType into ToPyType and FromPyType

    Split PyType into ToPyType and FromPyType

    Split PyType into ToPyType and FromPyType. This is nice to have if you only feel like implementing the conversion in one direction, or if there isn't a logical conversion in one of the directions.

    This PR breaks backward compatibility in the public API, so I'd appreciate a second opinion on it.

    opened by ghost 1
  • implement PyRun_* functions

    implement PyRun_* functions

    If we implement functions such as PyRun_StringFlags or PyRun_FileFlags, then it would be easy to use embedded python code in projects like video games.

    (sorry for stealing #1, I just wanted to say that this could be useful)

    opened by sinistersnare 1
  • Build fails on latest Rust nightly

    Build fails on latest Rust nightly

    $ rustc --version
    rustc 0.13.0-nightly (96991e933 2014-10-22 23:57:11 +0000)
    $ cargo build
       Compiling rustpy v0.1.0 (file:///home/d/Code/rustpy)
    src/primtypes.rs:152:5: 152:20 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
    src/primtypes.rs:152     self.as_slice().to_py_object(state)
                             ^~~~~~~~~~~~~~~
    src/primtypes.rs:151:3: 153:4 help: consider using an explicit lifetime parameter as shown: fn to_py_object<'a>(&'a self, state: &'a PyState) ->
     Result<PyObject<'a>, PyError>
    src/primtypes.rs:151   fn to_py_object<'a>(&self, state : &'a PyState) -> Result<PyObject<'a>, PyError> {
    src/primtypes.rs:152     self.as_slice().to_py_object(state)
    src/primtypes.rs:153   }
    error: aborting due to previous error
    Could not compile `rustpy`.
    
    bug 
    opened by ghost 0
Owner
Luke
Luke
Create, open, manage your Python projects with ease, a project aimed to make python development experience a little better

Create, open, manage your Python projects with ease, a project aimed to make python development experience a little better

Dhravya Shah 7 Nov 18, 2022
A cross-platform crate with FFI bindings to allow for complex vehicle ECU diagnostics.

ecu_diagnostics A cross-platform crate with FFI bindings to allow for complex vehicle ECU diagnostics. IMPORTANT Currently this crate is not 100% read

Ashcon Mohseninia 80 Dec 24, 2022
Squirt is a easy-to-use programming language.

Squirt is a easy-to-use programming language.

QuqqU 5 Nov 30, 2022
Robust and Fast tokenizations alignment library for Rust and Python

Robust and Fast tokenizations alignment library for Rust and Python Demo: demo Rust document: docs.rs Blog post: How to calculate the alignment betwee

Explosion 157 Dec 28, 2022
Very experimental Python bindings for the Rust biscuit-auth library

Overview This is a very experimental take on Python bindings for the biscuit_auth Rust library. It is very much a work in progress (limited testing, m

Josh Wright 5 Sep 14, 2022
Python bindings for heck, the Rust case conversion library

pyheck PyHeck is a case conversion library (for converting strings to snake_case, camelCase etc). It is a thin wrapper around the Rust library heck. R

Kevin Heavey 35 Nov 7, 2022
Arrowdantic is a small Python library backed by a mature Rust implementation of Apache Arrow

Welcome to arrowdantic Arrowdantic is a small Python library backed by a mature Rust implementation of Apache Arrow that can interoperate with Parquet

Jorge Leitao 52 Dec 21, 2022
Build a python wheel from a dynamic library

build_wheel Small utility to create a Python wheel given a pre-built dynamic library (.so, .dylib, .dll). If you are just trying to produce a wheel fr

Tangram 1 Dec 2, 2021
The polyglot bindings generator for your library (C#, C, Python, …) 🐙

Interoptopus ?? The polyglot bindings generator for your library. Interoptopus allows you to deliver high-quality system libraries to your users, and

Ralf Biedert 155 Jan 3, 2023
Rust <-> Python bindings

rust-cpython Rust bindings for the python interpreter. Documentation Cargo package: cpython Copyright (c) 2015-2020 Daniel Grunwald. Rust-cpython is l

Daniel Grunwald 1.7k Dec 29, 2022
Rust bindings for the Python interpreter

PyO3 Rust bindings for Python. This includes running and interacting with Python code from a Rust binary, as well as writing native Python modules. Us

PyO3 7.2k Jan 4, 2023
A script language like Python or Lua written in Rust, with exactly the same syntax as Go's.

A script language like Python or Lua written in Rust, with exactly the same syntax as Go's.

null 1.4k Jan 1, 2023
Rust Python modules for interacting with Metaplex's NFT standard.

Simple Metaplex Metadata Decoder Install the correct Python wheel for your Python version with pip: pip install metaplex_decoder-0.1.0-cp39-cp39-manyl

Samuel Vanderwaal 11 Mar 31, 2022
Implementation of Monte Carlo PI approximation algorithm in Rust Python bindings

rusty_pi Implementation of Monte Carlo PI approximation algorithm in Rust Python bindings. Time of 100M iterations approximation on Core i7 10th gen:

Aleksey Popov 1 Jul 6, 2022
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
RustPython - A Python Interpreter written in Rust

RustPython A Python-3 (CPython >= 3.9.0) Interpreter written in Rust ?? ?? ?? . Usage Check out our online demo running on WebAssembly. RustPython req

null 13.3k Jan 2, 2023
Python module implemented in Rust for counting the number of one bits in a numpy array.

bit-counter Package for counting the number of one bits in a numpy array of uint8 values. Implemented as a Python module using Rust, providing high pe

Andrew MacDonald 3 Jul 9, 2022
lavalink-rs bindings for Python

lavasnek_rs Dev Docs: Main Site | Fallback: GitHub Pages GitHub repo GitLab repo Using the library The library is available on PyPi, and you can insta

Victoria Casasampere Fernandez 39 Dec 27, 2022
Whitewash is python binding for Ammonia.

Whitewash Whitewash is python binding for Ammonia. Ammonia is a whitelist-based HTML sanitization library. It is designed to prevent cross-site script

Vivek Kushwaha 1 Nov 23, 2021