Skip to main content

Grafial Python Bindings

Python bindings for Grafial using PyO3 and maturin.

Install (end users)

pip install grafial

Requires CPython 3.11–3.13. Release CI publishes matching wheels for Linux (manylinux_2_17), macOS, and Windows for each of those interpreters (plus an sdist). After install:

import pathlib
import grafial

source = pathlib.Path("program.grafial").read_text()
program = grafial.compile(source)
ctx = grafial.run_flow(program, "MinimalFlow")
print(ctx.metrics)
print(ctx.inference_diagnostics)  # infer_beliefs convergence events, if any

Contributor / development build

Prerequisites

  • Rust toolchain (install via rustup)
  • Python 3.11 or later
  • maturin (pip install maturin or cargo install maturin)
  • Or uv for environment management (recommended)

Development Installation

Option 1: Using uv (recommended)

cd crates/grafial-python

# Create virtual environment
uv venv --python 3.11

# Activate virtual environment
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install in development mode (builds and installs the package)
uv pip install -e .

Option 2: Using maturin directly

cd crates/grafial-python

# Create virtual environment (or use existing)
python3 -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install maturin if not already installed
pip install maturin

# Build and install in development mode
maturin develop --release

Option 3: Using nix-shell

If you're using the project's shell.nix:

# Enter nix shell (from project root)
nix-shell

# Then follow Option 1 or 2 above
cd crates/grafial-python
uv pip install -e .  # or maturin develop --release

Running Tests

After installation, you can run Python tests:

# From crates/grafial-python directory
.venv/bin/pytest tests/

# Or run specific test file
.venv/bin/pytest tests/test_basic.py -v

You can also run Rust lint checks for the bindings crate:

# From project root
cargo clippy -p grafial-python --all-targets -- -D warnings

Using in Python

Once installed, you can import and use Grafial:

import grafial

# Compile a Grafial program
source = """
schema Test {
    node Person { }
    edge KNOWS { }
}
belief_model TestBeliefs on Test {
    edge KNOWS { exist ~ Bernoulli(prior=0.5, weight=2.0) }
}
"""
program = grafial.compile(source)

# Check what's in the program
print(f"Schemas: {program.get_schema_names()}")
print(f"Models: {program.get_belief_model_names()}")

Building for Distribution

Building Wheels

To build Python wheels for distribution:

cd crates/grafial-python

# Build wheels for current platform
maturin build --release

# Build wheels for multiple platforms (requires cross-compilation setup)
maturin build --release --out dist

This creates .whl files in the dist/ directory that can be installed with pip.

Installing from Wheel

pip install dist/grafial-*.whl

Building Source Distribution

maturin sdist

This creates a source distribution that can be built on any platform.

Publishing to PyPI

Prerequisites

  1. Create accounts on PyPI and TestPyPI
  2. Configure credentials (using twine or maturin)

Publishing

1. Test on TestPyPI first:

maturin publish --test-pypi

2. Publish to PyPI:

maturin publish

Maturin will automatically:

  • Build wheels for multiple Python versions
  • Build source distribution
  • Upload to PyPI

Installing from PyPI (after publishing)

pip install grafial

Using in Other Python Projects

Option 1: Install from Local Path

# In your Python project
pip install /path/to/grafial/crates/grafial-python

Option 2: Install from Git Repository

pip install git+https://github.com/iridae-dev/grafial.git#subdirectory=crates/grafial-python

Option 3: Add as Dependency in pyproject.toml

[project]
dependencies = [
    "grafial @ git+https://github.com/iridae-dev/grafial.git#subdirectory=crates/grafial-python",
]

Option 4: Development Editable Install

For active development on both projects:

# In your Python project's virtual environment
pip install -e /path/to/grafial/crates/grafial-python

This installs in "editable" mode, so changes to the Rust code will be reflected after rebuilding (run maturin develop again).

Troubleshooting

Build Errors

"Can't find Python"

  • Set PYO3_PYTHON environment variable: export PYO3_PYTHON=$(which python3)
  • Or use maturin develop --python python3 to specify Python version

"Missing Rust toolchain"

  • Install Rust: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • Ensure cargo is in your PATH

"maturin not found"

  • Install maturin: pip install maturin or cargo install maturin
  • Or use uv pip install maturin if using uv

Import Errors

"ModuleNotFoundError: No module named 'grafial'"

  • Ensure you've installed the package: pip install -e . or maturin develop
  • Check you're using the correct virtual environment
  • Verify installation: pip list | grep grafial

"ImportError: dynamic module does not define module export function"

  • Rebuild the extension: maturin develop --release
  • This usually happens when Rust code changed but wasn't rebuilt

Testing Issues

Tests fail with "library not loaded" errors

  • Rebuild: maturin develop --release
  • Ensure Python version matches the one used to build

Development Workflow

Making Changes

  1. Edit Rust code in src/lib.rs
  2. Rebuild: maturin develop --release (or uv pip install -e .)
  3. Test: .venv/bin/pytest tests/
  4. Iterate

Debugging

For faster iteration during development, use debug builds:

maturin develop  # Without --release (faster, but slower runtime)

For production-like testing:

maturin develop --release  # Optimized build (slower build, faster runtime)

Code Generation

Maturin automatically:

  • Compiles Rust code to a Python extension module
  • Generates Python type stubs (.pyi files) if configured
  • Handles linking and platform-specific details

Project Structure

crates/grafial-python/
├── Cargo.toml          # Rust package configuration
├── pyproject.toml      # Python package configuration (maturin)
├── src/
│   └── lib.rs          # PyO3 bindings code
├── tests/              # Python tests
│   ├── test_basic.py
│   ├── test_flow_minimal.py
│   └── ...
└── README.md           # This file

Resources

Metadata

Release files for grafial 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grafial 0.2.1
File Size Uploaded Provenance Actions
grafial-0.2.1.tar.gz 263.8 kB Without provenance Details for grafial-0.2.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for grafial 0.2.1
File Actions
grafial-0.2.1-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for grafial-0.2.1-cp313-cp313-win_amd64.whl
grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.17+ x86-64 Details for grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
grafial-0.2.1-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for grafial-0.2.1-cp312-cp312-win_amd64.whl
grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details for grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
grafial-0.2.1-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for grafial-0.2.1-cp311-cp311-win_amd64.whl
grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details for grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl

Total release size: 8.2 MB

Release files / grafial-0.2.1.tar.gz

Download URL grafial-0.2.1.tar.gz
Size 263.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bba11626b228c1aa80043561e67acc0723dc687425f6e0343f86daa5fba2e2b6
BLAKE2b-256 checksum
How to use checksums
89f6456421a2056593b69b384c5aeb9763d4ccf633c35515b77bb2ba27a5f649
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp313-cp313-win_amd64.whl

Download URL grafial-0.2.1-cp313-cp313-win_amd64.whl
Size 803.8 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
b706f25c58da61e776fa078b50d098ec400581d875d17fafa69bbe96a7fd6b1e
BLAKE2b-256 checksum
How to use checksums
99f1c7a163eac0eab334024550ec8c17738bf3e226ba3e380c3eae6463f3d419
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 957.4 kB
Tags CPython 3.13 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
8e07a3445f12c23a3da71747ada5874317d49e09cb58ce65aa400964a274f0f1
BLAKE2b-256 checksum
How to use checksums
a90e25157d0ffaf5f3e3deabe51e1c966ceeaff79403c10b8bee126d4ac2a4e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl

Download URL grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
Size 879.3 kB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0e7427eb016824bc58bf007438d0fa7ed933e64cbb31e47af2de4283a347ae23
BLAKE2b-256 checksum
How to use checksums
d32fe7384fc4f9cf35023e79cf626620beaf922cc059b67c17494b483aeacccf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp312-cp312-win_amd64.whl

Download URL grafial-0.2.1-cp312-cp312-win_amd64.whl
Size 804.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
bd8fd7fbc41fb0d7663ac68a786891c29a8693b775c05fe84fc8ded4037019ec
BLAKE2b-256 checksum
How to use checksums
075c44a122dabc267cc2de24d4146317fcb18e5300112bdb35fa2c9790ed337b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 957.9 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
ecc728fa2fb26dd063dac02e00ce3b336f26b28bda6f5f157384c2bec826bd4a
BLAKE2b-256 checksum
How to use checksums
1c959d80266a8042666bbcb630617ad14b45de2a3d7dbc61284d116c478c53ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl

Download URL grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
Size 879.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7401aa6097837a51c4686c548b643d08777a9774a822f86da016ec6d07677e84
BLAKE2b-256 checksum
How to use checksums
0e94ec702bc0d3e6f8067cac674873f8cab8bbaedbc5e7f7353e17a2572e3a69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp311-cp311-win_amd64.whl

Download URL grafial-0.2.1-cp311-cp311-win_amd64.whl
Size 805.5 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
54be70b19d017d68a5d7b06207ac487523e660e45ea221d654a9a528c70e60b2
BLAKE2b-256 checksum
How to use checksums
35695d0d7e8e7a0b184ddd209a11acfc4ddcd7bfe6edf1fcbb37b6b60e9c5d77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 958.0 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5007bfe6b285152fb2f8242016800940289b6788c5cdb0562e598bcc19a0322a
BLAKE2b-256 checksum
How to use checksums
164edb12174156f77f8b5fc9c871f705410d5662b7f0285024c59f71aac57b10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl

Download URL grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
Size 880.1 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e7e9fffed5458f44fae751852800e27904b1d817507950a02ebfa62ceddd1793
BLAKE2b-256 checksum
How to use checksums
54b3eaa6ee29e02eb326ac3d612138383f5d032ea317ab85ddb89c879a052b7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for grafial 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page