Skip to main content

Python bindings for sparse-ir-capi

This is a low-level binding for the sparse-ir-capi Rust library.

Requirements

  • Python >= 3.10
  • Rust toolchain (for building the Rust library)
  • uv (for building and managing Python dependencies)
  • numpy >= 1.26.4
  • scipy

BLAS Support

This package automatically uses SciPy's BLAS backend for optimal performance. No additional BLAS installation is required - SciPy will provide the necessary BLAS functionality.

Build

Install Dependencies and Build

# Build the package (Rust library will be built automatically)
cd python
uv build

This will:

  • Automatically build the Rust sparse-ir-capi library with Cargo (through the hatchling build hook in hatch_build.py)
  • Copy the built library and header file to the Python package
  • Create both source distribution (sdist) and wheel packages

Development Build

For development:

# Install in development mode (will auto-prepare if needed)
uv sync --locked

Note for CI/CD: The Rust library is built automatically during the Python package build. No separate build step is needed:

# In CI/CD scripts
cd python
uv build

See .github/workflows/ in the repository for the workflows that build and test this package.

BLAS Configuration

The package automatically uses SciPy's BLAS backend, which provides optimized BLAS operations without requiring separate BLAS installation. The build system is configured to use SciPy's BLAS functions directly.

Clean Build Artifacts

To remove build artifacts and files copied from the parent directory:

make clean

This will remove:

  • Build directories and caches: build/, dist/, *.egg-info, __pycache__/, .pytest_cache/, .venv/
  • Files copied into the package: pylibsparseir/*.so, pylibsparseir/*.dylib, pylibsparseir/*.dll, pylibsparseir/*.h

Build Process Overview

The build process works as follows:

  1. Build hook: pyproject.toml uses the hatchling backend, which runs the custom build hook in hatch_build.py.

  2. Rust Library Build: the hook runs cargo build --release -p sparse-ir-capi from the repository root:

    • Compiles the Rust library to a shared library (.so, .dylib, or .dll)
    • Removes old shared libraries from pylibsparseir/, then copies the new library and the header sparse-ir-capi/include/sparseir/sparseir.h there
  3. Python Package Building: uv build or uv sync --locked:

    • Packages everything into distributable wheels and source distributions
  4. Installation: The built package includes the compiled shared library and Python bindings

Conda Build

This package can also be built and distributed via conda-forge. The conda recipe is located in conda-recipe/ and supports multiple platforms and Python versions.

Building conda packages locally:

# Install conda-build
conda install conda-build

# Build the conda package
cd python
conda build conda-recipe

# Build for specific platforms
conda build conda-recipe --platform linux-64
conda build conda-recipe --platform osx-64
conda build conda-recipe --platform osx-arm64

Supported platforms:

  • Linux x86_64
  • macOS Intel (x86_64)
  • macOS Apple Silicon (ARM64)

Supported Python versions:

  • Python 3.11, 3.12, 3.13, 3.14 (the conda recipe; the PyPI package itself supports Python 3.10 and newer, see pyproject.toml)

Supported NumPy versions:

  • NumPy 2.1, 2.2, 2.3

The conda build automatically:

  • Uses SciPy's BLAS backend for optimal performance
  • Cleans up old shared libraries before building
  • Builds platform-specific packages with proper dependencies

Handle Ownership

Every function in pylibsparseir.core that creates a C object returns an owning handle instead of a raw ctypes pointer: KernelHandle, SVEResultHandle, BasisHandle, FuncsHandle, SamplingHandle, or GemmBackendHandle (the default BLAS backend returned by get_default_blas_backend()).

  • A handle is released exactly once with the matching spir_*_release: when it becomes unreachable, when close() is called (also on leaving a with handle: block), or at interpreter exit. Closing it again does nothing.
  • An open handle can be passed directly to any _lib.spir_* function and is truthy. A released handle is falsy, and passing it to C raises ctypes.ArgumentError instead of handing C a freed pointer.
  • Do not release an owned handle through _lib.spir_*_release. Those entry points refuse owned handles with ctypes.ArgumentError, because the owner would free them a second time. Handles created directly through _lib remain the caller's to release, or can be handed over to an owner, e.g. FuncsHandle(_lib.spir_funcs_clone(funcs)).
  • The DLR and MiniPole entry points have no wrapper in pylibsparseir.core; call them through _lib (spir_dlr_new_independent, spir_dlr_new, spir_minipole_from_dlr, spir_minipole_from_matsubara). A DLR is a spir_basis and can be handed to BasisHandle. A spir_pole_repr has no owning handle class, so release it yourself with _lib.spir_pole_repr_release.
  • C handles never depend on the handles they were created from (spir_basis_new copies the kernel and the SVE result; funcs and samplings own their data), so handles can be released in any order.

Performance Notes

BLAS Support

This package automatically uses SciPy's optimized BLAS backend for improved linear algebra performance:

  • Automatic BLAS: Uses SciPy's BLAS functions for optimal performance
  • No additional setup: SciPy provides all necessary BLAS functionality

The build system automatically configures BLAS support through SciPy. You can verify BLAS support with debug output enabled:

export SPARSEIR_DEBUG=1
python -c "import pylibsparseir"

This will show:

[core.py] Created SciPy BLAS backend handle
[core.py] Registered SciPy BLAS dgemm @ 0x...
[core.py] Registered SciPy BLAS zgemm @ 0x...

Debug Output

The SPARSEIR_DEBUG environment variable enables debug output only when it is set to 1, true, yes or on, in any letter case. Any other value, including 0, false or an empty string, disables it, as does leaving it unset. The same rule applies to both layers:

  • pylibsparseir prints its messages to stdout. It checks the variable when it is imported.
  • The Rust library prints [SPARSEIR DEBUG], [SPARSEIR DEBUG ERROR] and [SPARSEIR WARN] lines to stderr. It checks the variable each time it would print one.

Troubleshooting

Build fails with missing Cargo:

# Make sure Rust toolchain is installed
# Install from https://rustup.rs/
# Then retry:
cd python
uv build

Clean rebuild:

# Remove all build artifacts
make clean
cd ../sparse-ir-capi
cargo clean
cd ../python
uv build

Metadata

Release files for pylibsparseir 0.13.0

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

Built distributions (wheels)

Table of built distributions (wheels) for pylibsparseir 0.13.0
File
pylibsparseir-0.13.0-cp314-cp314-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64 Details
pylibsparseir-0.13.0-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details
pylibsparseir-0.13.0-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details
pylibsparseir-0.13.0-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details
pylibsparseir-0.13.0-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details
pylibsparseir-0.13.0-cp312-cp312-macosx_15_0_arm64.whl CPython 3.12 CPython 3.12 macOS 15.0+ ARM64 Details
pylibsparseir-0.13.0-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details
pylibsparseir-0.13.0-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details
pylibsparseir-0.13.0-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details
pylibsparseir-0.13.0-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details

Total release size: 66.8 MB

Release files / pylibsparseir-0.13.0-cp314-cp314-manylinux_2_28_x86_64.whl

Download URL pylibsparseir-0.13.0-cp314-cp314-manylinux_2_28_x86_64.whl
Size 7.2 MB
Tags CPython 3.14 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
2dcbc5f7146e8bbfa5bf6a4e4ba4de51d12295fef3862ac2d90087e54d3b6d1a
BLAKE2b-256 checksum
How to use checksums
c9051daa09a0172d6e3b6002919ab7b98919be216aa27cadb614c6aac0d2c477
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp314-cp314-macosx_15_0_arm64.whl

Download URL pylibsparseir-0.13.0-cp314-cp314-macosx_15_0_arm64.whl
Size 6.1 MB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
32867ac38085d26bef70687e5d4ddd7cf78df901f815a00a08308def811dd84f
BLAKE2b-256 checksum
How to use checksums
a8b2075d47d6e0cbbac770a3a009933494bd37faff23f67a232efdca91040396
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL pylibsparseir-0.13.0-cp313-cp313-manylinux_2_28_x86_64.whl
Size 7.2 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
bf156b77c924f4b0ae66640348aa1df82ab69f2221f42436875b4a2873bc27cd
BLAKE2b-256 checksum
How to use checksums
c77cbd32ec963163cddc6dd5beb6891e7fb3e8fa17d4956426103d7bddcf5dfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp313-cp313-macosx_15_0_arm64.whl

Download URL pylibsparseir-0.13.0-cp313-cp313-macosx_15_0_arm64.whl
Size 6.1 MB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
728184cf43687dd93975900ba72ee17b1bada43bc8acee1773fc06f64d0413b9
BLAKE2b-256 checksum
How to use checksums
448f6710c28861b98bb2d6b1459ba1c4eb1b65c57927e518470fadf1f9f8eb4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL pylibsparseir-0.13.0-cp312-cp312-manylinux_2_28_x86_64.whl
Size 7.2 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
56edbcefd2bf3caa00a50c6110fda7483efbbfe6fc314545451aba97dd04382e
BLAKE2b-256 checksum
How to use checksums
2b73f329294f15dbff3515a89da322783de03f2ff7feabb00ac7d801f30340b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp312-cp312-macosx_15_0_arm64.whl

Download URL pylibsparseir-0.13.0-cp312-cp312-macosx_15_0_arm64.whl
Size 6.1 MB
Tags CPython 3.12 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
ea8117fbc7c7087952af7097e247b2d6053e3b382bab975082953c54edec454f
BLAKE2b-256 checksum
How to use checksums
36b93b50ed811d66fe309252aad51c7053c7df30d534effde2c045d534bfb5a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL pylibsparseir-0.13.0-cp311-cp311-manylinux_2_28_x86_64.whl
Size 7.2 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
117db4a65a713d1c46247d7e3638bd8cab1c15b1b7834a24bd0e2436e727d36d
BLAKE2b-256 checksum
How to use checksums
5f7a47fbcd8ea098173223ff0c5cd58c3803ac62d9fa5838692eda944406c28c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp311-cp311-macosx_15_0_arm64.whl

Download URL pylibsparseir-0.13.0-cp311-cp311-macosx_15_0_arm64.whl
Size 6.1 MB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
18b659a815d848ba1054568052470ebdb815ae2d608e59a60e1f7f0981ffd3bf
BLAKE2b-256 checksum
How to use checksums
57a530d789e9207d7416179b4919dc65932fa617f673706e232d164aaee49b60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL pylibsparseir-0.13.0-cp310-cp310-manylinux_2_28_x86_64.whl
Size 7.2 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
93cfc837305ed4d631dcfc9cc7a63bbfacc5403502893366c29d4b65d497314c
BLAKE2b-256 checksum
How to use checksums
62be4d34ed5c35bed20f2f94a9f66d65cad95146b1621fb768ef9ba3839aec53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / pylibsparseir-0.13.0-cp310-cp310-macosx_15_0_arm64.whl

Download URL pylibsparseir-0.13.0-cp310-cp310-macosx_15_0_arm64.whl
Size 6.1 MB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
5169c2459ab5a6943d5be53e7e999e4aa84af2406d28b6f2db0e9f6389c35233
BLAKE2b-256 checksum
How to use checksums
7f94e0b0cacb3fa5df7d9bf0ce41cc4cba71b0beb3554c3e6266a1f3cb0433e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.13.0 This release

10 release files

0.9.1

10 release files

0.9.0

10 release files

0.8.4

8 release files

0.8.3

8 release files

0.8.2

8 release files

0.8.0

8 release files

0.7.5

8 release files

0.7.4

8 release files

0.7.3

8 release files

0.6.0

16 release files

0.5.2

16 release files

0.1.0

16 release files

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