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.16.1

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.16.1
File Actions
pylibsparseir-0.16.1-cp314-cp314-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64 Details for pylibsparseir-0.16.1-cp314-cp314-manylinux_2_28_x86_64.whl
pylibsparseir-0.16.1-cp314-cp314-macosx_15_0_arm64.whl CPython 3.14 CPython 3.14 macOS 15.0+ ARM64 Details for pylibsparseir-0.16.1-cp314-cp314-macosx_15_0_arm64.whl
pylibsparseir-0.16.1-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details for pylibsparseir-0.16.1-cp313-cp313-manylinux_2_28_x86_64.whl
pylibsparseir-0.16.1-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details for pylibsparseir-0.16.1-cp313-cp313-macosx_15_0_arm64.whl
pylibsparseir-0.16.1-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details for pylibsparseir-0.16.1-cp312-cp312-manylinux_2_28_x86_64.whl
pylibsparseir-0.16.1-cp312-cp312-macosx_15_0_arm64.whl CPython 3.12 CPython 3.12 macOS 15.0+ ARM64 Details for pylibsparseir-0.16.1-cp312-cp312-macosx_15_0_arm64.whl
pylibsparseir-0.16.1-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details for pylibsparseir-0.16.1-cp311-cp311-manylinux_2_28_x86_64.whl
pylibsparseir-0.16.1-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details for pylibsparseir-0.16.1-cp311-cp311-macosx_15_0_arm64.whl
pylibsparseir-0.16.1-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details for pylibsparseir-0.16.1-cp310-cp310-manylinux_2_28_x86_64.whl
pylibsparseir-0.16.1-cp310-cp310-macosx_15_0_arm64.whl CPython 3.10 CPython 3.10 macOS 15.0+ ARM64 Details for pylibsparseir-0.16.1-cp310-cp310-macosx_15_0_arm64.whl

Total release size: 67.2 MB

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

Download URL pylibsparseir-0.16.1-cp314-cp314-manylinux_2_28_x86_64.whl
Size 7.3 MB
Tags CPython 3.14 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
145b31a246176e4f93ea7f06c4e73d13dcfb1a6c76bb252555b6f11c56e41ec1
BLAKE2b-256 checksum
How to use checksums
efa551f6aedab7b48328e944b4d65f63052a33242cafe6daae25ddbad6b20226
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp314-cp314-macosx_15_0_arm64.whl
Size 6.2 MB
Tags CPython 3.14 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
c1e7610e340156b074f27eecd65d12979e3113d1ad3e851fd0421a2a6f6a33f1
BLAKE2b-256 checksum
How to use checksums
ab16ac4a7790e15eed136438894f27e091dee0577c3470a86e2238d5551efe1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

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

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

Download URL pylibsparseir-0.16.1-cp313-cp313-macosx_15_0_arm64.whl
Size 6.2 MB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
2a8690d4c64a26d4484a1718f64f41693b307db7d4b78203978abc01f32c9855
BLAKE2b-256 checksum
How to use checksums
eda45006b77806717113b01d56e350f2621c23a7a8293163a1a09ed05965c699
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp312-cp312-manylinux_2_28_x86_64.whl
Size 7.3 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8d8491bb62922c1d90f818a1d2075eafa0ba0664a36f464a2d0b186e6065eaea
BLAKE2b-256 checksum
How to use checksums
a4a84d268bba6d0ccba453954bf9f29437d6768d7f38b8af9a2d16932929d3c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp312-cp312-macosx_15_0_arm64.whl
Size 6.2 MB
Tags CPython 3.12 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
2ef0bc5f6c4c3a8bd8f7eb2727e5d73685a31902b218392cc4fcd602abfb7dea
BLAKE2b-256 checksum
How to use checksums
f527491c70144b91c42278ba4f52d6d69cb5d0d3d4f7feb6c9f3f76d17b2ed4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp311-cp311-manylinux_2_28_x86_64.whl
Size 7.3 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8f6e3fb159e69b01d38f6e389eea056283618373eee430e56ee2ae6ff8d5f6ec
BLAKE2b-256 checksum
How to use checksums
7e3b0b5fd7f974ea8612c955026ed9ec73c5ceafa32376b765afde5520e5152a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp311-cp311-macosx_15_0_arm64.whl
Size 6.2 MB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
048e509b214592ece60e12879216cbecf46eb1dc9e9f1e5d30d7ad38dbf425ff
BLAKE2b-256 checksum
How to use checksums
2150faad84d93bde7eb0cc0b62cc8dc56a2989b9d1eb4b2f6a8a9c2c7c981e22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp310-cp310-manylinux_2_28_x86_64.whl
Size 7.3 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
37bbf7f84f2699c5bdf451c72b10019670bd0fbfedea6735faa1e866479c6104
BLAKE2b-256 checksum
How to use checksums
e9d45f382e6dbdc3c4f5606893aeafed84cd6b8c87d970e3f9f2946d1ab69679
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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

Download URL pylibsparseir-0.16.1-cp310-cp310-macosx_15_0_arm64.whl
Size 6.2 MB
Tags CPython 3.10 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
41fc88ff13f3a4da2050908b8dbf85cce57ad9ddf7cd5118401e0f7ca835076d
BLAKE2b-256 checksum
How to use checksums
197fd41252ea7142db8b7da714b6047c91f2e58eaf2ea1f21533d53c35c23a08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Security for pylibsparseir 0.16.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