/python-rust

Python extensions in Rust

Primary LanguageRustMIT LicenseMIT

Python extensions in Rust

build

Template repo for writing Python extensions in Rust.

Based on documentation at https://www.maturin.rs/index.html.

Getting started

$ python -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip install .
(.venv) $ python
>>> from python_lib import StateMachine, par_run
>>> machines = [StateMachine for _ in range(100)]
>>> _ = par_run(machines)

Project layout

The src/python folder contains example Python objects written in Rust. These are exported into the Python extension module in src/python_module.rs.

Development

Setup the development environment

Linux and macOS

  1. Install Rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. Install pyenv: curl https://pyenv.run | bash
  3. Install Python interpreter(s) listed in the file .python-version:
while read line; do
  pyenv install "$line"
done < .python-version
  1. Create a virtual environment: python3 -m venv .venv
  2. Activate the venv: source .venv/bin/activate
  3. Install the dependencies: pip install -e .[develop]

Testing and linting

Rust

Run tests:

cargo test

Run linters:

cargo fmt -v --check
cargo clippy

Python

Run all tests and linters:

tox

Run specific linters:

# Black, isort, mypy, pylint
tox -e black
# etc. ...

Formatting

Rust

cargo fmt

Python

tox -e format