Python bindings for Grafial - Bayesian belief graph inference DSL
Project description
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 maturinorcargo 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
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_PYTHONenvironment variable:export PYO3_PYTHON=$(which python3) - Or use
maturin develop --python python3to specify Python version
"Missing Rust toolchain"
- Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh - Ensure
cargois in your PATH
"maturin not found"
- Install maturin:
pip install maturinorcargo install maturin - Or use
uv pip install maturinif using uv
Import Errors
"ModuleNotFoundError: No module named 'grafial'"
- Ensure you've installed the package:
pip install -e .ormaturin 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
- Edit Rust code in
src/lib.rs - Rebuild:
maturin develop --release(oruv pip install -e .) - Test:
.venv/bin/pytest tests/ - 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 (
.pyifiles) 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file grafial-0.2.1.tar.gz.
File metadata
- Download URL: grafial-0.2.1.tar.gz
- Upload date:
- Size: 263.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bba11626b228c1aa80043561e67acc0723dc687425f6e0343f86daa5fba2e2b6
|
|
| MD5 |
c673fd9653c8accad2a3ae22a15f68aa
|
|
| BLAKE2b-256 |
89f6456421a2056593b69b384c5aeb9763d4ccf633c35515b77bb2ba27a5f649
|
File details
Details for the file grafial-0.2.1-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: grafial-0.2.1-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 803.8 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b706f25c58da61e776fa078b50d098ec400581d875d17fafa69bbe96a7fd6b1e
|
|
| MD5 |
d56117a28c80b0c016cdc6e74942fdf8
|
|
| BLAKE2b-256 |
99f1c7a163eac0eab334024550ec8c17738bf3e226ba3e380c3eae6463f3d419
|
File details
Details for the file grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: grafial-0.2.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 957.4 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e07a3445f12c23a3da71747ada5874317d49e09cb58ce65aa400964a274f0f1
|
|
| MD5 |
d9b795258ba25943eda41a290886f412
|
|
| BLAKE2b-256 |
a90e25157d0ffaf5f3e3deabe51e1c966ceeaff79403c10b8bee126d4ac2a4e0
|
File details
Details for the file grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: grafial-0.2.1-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 879.3 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e7427eb016824bc58bf007438d0fa7ed933e64cbb31e47af2de4283a347ae23
|
|
| MD5 |
a2f281ea03769431b455cc4af97f4272
|
|
| BLAKE2b-256 |
d32fe7384fc4f9cf35023e79cf626620beaf922cc059b67c17494b483aeacccf
|
File details
Details for the file grafial-0.2.1-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: grafial-0.2.1-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 804.3 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd8fd7fbc41fb0d7663ac68a786891c29a8693b775c05fe84fc8ded4037019ec
|
|
| MD5 |
5b747627e900f65f186ef7dbaf9e94ac
|
|
| BLAKE2b-256 |
075c44a122dabc267cc2de24d4146317fcb18e5300112bdb35fa2c9790ed337b
|
File details
Details for the file grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: grafial-0.2.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 957.9 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ecc728fa2fb26dd063dac02e00ce3b336f26b28bda6f5f157384c2bec826bd4a
|
|
| MD5 |
920b5e24215df2cf80b699df0eea3dde
|
|
| BLAKE2b-256 |
1c959d80266a8042666bbcb630617ad14b45de2a3d7dbc61284d116c478c53ed
|
File details
Details for the file grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: grafial-0.2.1-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 879.6 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7401aa6097837a51c4686c548b643d08777a9774a822f86da016ec6d07677e84
|
|
| MD5 |
95d9a0eed5a828269d2640e835e296cd
|
|
| BLAKE2b-256 |
0e94ec702bc0d3e6f8067cac674873f8cab8bbaedbc5e7f7353e17a2572e3a69
|
File details
Details for the file grafial-0.2.1-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: grafial-0.2.1-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 805.5 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54be70b19d017d68a5d7b06207ac487523e660e45ea221d654a9a528c70e60b2
|
|
| MD5 |
8975fa97fafe5ea7e1a81853debd95fa
|
|
| BLAKE2b-256 |
35695d0d7e8e7a0b184ddd209a11acfc4ddcd7bfe6edf1fcbb37b6b60e9c5d77
|
File details
Details for the file grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: grafial-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 958.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5007bfe6b285152fb2f8242016800940289b6788c5cdb0562e598bcc19a0322a
|
|
| MD5 |
76f329d787becdaedcbb292049818073
|
|
| BLAKE2b-256 |
164edb12174156f77f8b5fc9c871f705410d5662b7f0285024c59f71aac57b10
|
File details
Details for the file grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: grafial-0.2.1-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 880.1 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7e9fffed5458f44fae751852800e27904b1d817507950a02ebfa62ceddd1793
|
|
| MD5 |
d63a46b8ec7a1e62969cb322a4c522b5
|
|
| BLAKE2b-256 |
54b3eaa6ee29e02eb326ac3d612138383f5d032ea317ab85ddb89c879a052b7c
|