Skip to main content

Python runtime + builder for calling Go shared libraries directly (MessagePack ABI).

Project description

usegolib

CI PyPI Python Versions License

Call Go libraries from Python by loading a Go-built shared library directly into the Python process.

  • No C/C++ glue code written by you
  • No background Go process (no daemon / RPC / subprocess)
  • MessagePack-based ABI with manifest-driven schema validation

Status

usegolib is early-stage software (v0.x). Expect sharp edges and evolving interfaces.

How It Works

  • Build time (CI / build machine): use the Go toolchain plus an auto-provisioned C compiler (Zig) to build a Go module into a shared library and emit manifest.json.
  • Runtime (end-user / production): Python loads the shared library and calls it via MessagePack. No daemon, no RPC, no subprocess.

Requirements

  • Python 3.10+
  • Go toolchain
    • required for building artifacts (CI / build machine)
    • required in dev if you rely on auto-build (missing artifact triggers a build)
    • not required for end-user runtime if you ship prebuilt artifacts or wheels

Installation

python -m pip install usegolib

From source (dev):

python -m pip install -e ".[dev]"

Quickstart

Build an artifact from a local Go module directory:

usegolib build --module path/to/go/module --out out/artifact

Import from the artifact root and call exported functions:

import usegolib

h = usegolib.import_("example.com/mod", artifact_dir="out/artifact")
print(h.AddInt(1, 2))

Dev convenience: omit artifact_dir to use the default artifact cache root. If the artifact is missing, usegolib.import_ will build it into the default cache when a Go toolchain is available.

import usegolib

h = usegolib.import_("example.com/mod")
print(h.AddInt(1, 2))

To enforce "no build" behavior, pass an explicit artifact root and set build_if_missing=False:

import usegolib

h = usegolib.import_("example.com/mod", artifact_dir="out/artifact", build_if_missing=False)

Example: Calling Insyra From Python

Insyra is a Go data-analysis library: github.com/HazelnutParadise/insyra.

Auto-build on import (developer convenience; downloads the module + builds into usegolib's default artifact root):

import usegolib

insyra = usegolib.import_("github.com/HazelnutParadise/insyra", version="v0.2.14")

# DataList (object handle + variadic any)
dl = insyra.NewDataList()
dl.Append(1, 2, 3, 4, 5)
print(dl.Sum(), dl.Mean())

# DataTable (variadic map[string]any + variadic bool)
dt = insyra.NewDataTable()
dt.AppendRowsByColIndex({"A": 1, "B": 2}, {"A": 3, "B": 4})
print(dt.NumRows(), dt.NumCols())
dt.Show()

Notes:

  • usegolib can only call functions/methods whose parameter and return types are supported by the current type bridge (see docs/abi.md).
  • This example requires a Go toolchain to be installed. (At the time of writing, insyra@v0.2.14 requires go >= 1.25 and may cause Go to auto-download a newer toolchain.)
  • If you want to disable auto-build, pass build_if_missing=False (and usually an explicit artifact_dir).
  • For end-users without Go, ship prebuilt artifacts/wheels (see Packaging).

Methods (Object Handles)

For exported methods, usegolib supports Go-side object handles (opaque ids) to avoid serializing the receiver on every call:

with h.object("Counter", {"n": 1}) as c:
    print(c.Inc(2))

Typed object handles (schema required):

th = h.typed()
types = th.types

with th.object("Counter", types.Counter(N=10)) as c:
    snap = c.Snapshot()
    print(snap)

Generic Functions (Build-Time Instantiation)

Generic functions require explicit build-time instantiation:

usegolib build --module example.com/mod --out out/artifact --generics generics.json

At runtime (schema required), use the helper:

fn = h.generic("Id", ["int64"])
print(fn(123))

Packaging (Ship Wheels Without Requiring Go)

Generate a distributable Python package project embedding artifacts:

usegolib package --module path/to/go/module --python-package-name mypkg --out out/

Install the generated project:

python -m pip install -e out/mypkg
import mypkg

print(mypkg.AddInt(1, 2))

Version Rules

  • One Go module = one version per Python process
  • If a module is already loaded, all subpackages must use the same version
  • If version=None and multiple artifact versions exist under artifact_dir, import fails with AmbiguousArtifactError (unless that module is already loaded in the current process, in which case version=None follows the already-loaded version)

Documentation

  • docs/roadmap.md
  • docs/abi.md
  • docs/cli.md
  • docs/versioning.md
  • docs/compatibility.md
  • docs/testing.md
  • docs/security.md
  • docs/troubleshooting.md
  • docs/releasing.md

License

MIT. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

usegolib-0.1.0.tar.gz (75.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

usegolib-0.1.0-py3-none-any.whl (64.8 kB view details)

Uploaded Python 3

File details

Details for the file usegolib-0.1.0.tar.gz.

File metadata

  • Download URL: usegolib-0.1.0.tar.gz
  • Upload date:
  • Size: 75.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for usegolib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f1d47d9c3453c5f2c5d3352cc8214ef56406ffd730ff43a3859b61bae0ffbbb
MD5 5fc732ab37b391d4c4a63c109e76176f
BLAKE2b-256 8a8d9d7b96e163a93da60f9f214988715e2580c3832435fd3ccf943148058f28

See more details on using hashes here.

Provenance

The following attestation bundles were made for usegolib-0.1.0.tar.gz:

Publisher: publish-pypi.yml on TimLai666/usegolib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file usegolib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: usegolib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 64.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for usegolib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7dc7226ce82deaac463964e183f5f571e54ba39d0f10e03ecf8451e22720d58
MD5 325121c189c943c62a97119db82c068d
BLAKE2b-256 a3772659a942d1a9c7290640769d09390527cd33eecef206738e495cd9d26a37

See more details on using hashes here.

Provenance

The following attestation bundles were made for usegolib-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on TimLai666/usegolib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page