Skip to main content

Include Go packages as a Python extension module

Project description

go-extension

go-extension provides GoExtension class that is given as one of ext_modules argument to setuptools.setup() to generate and compile a CPython extension module from Go pacakges.

Requirements

  • Python3.10 or later
  • Go1.16 or later (in order to build)

Installation

$ pip install go-extension

Getting Started

from setuptools import setup
from go_extension import GoExtension

ext = GoExtension(
    name='hello_go',
    packages=['github.com/huisint/go-extension-python/hello']
)

import sys
sys.argv = ['', 'build_ext', '--inplace'] # To call `build_ext` command of setup().
setup(ext_modules=[ext])

from hello_go import hello
print()
hello.hello_world() # print "Hello, World"
hello.greet('Go')   # print "Hello, Go"

Go package in Python project root

Now you have py_pkg and go_pkg in project root. The following shows how to build go_pkg as a Python package named py_pkg/go.

project-root
│
├─go_pkg
│   └─main.go # package hello
│
└─py_pkg
    └─__init__.py

First, init your project root as Go module by running:

$ go mod init example.com/foo/bar

Second, create setup.py like this:

from setuptools import setup
from pathlib import Path
try:
    from go_extension import GoExtension
except (ImportError, ModuleNotFoundError):
    from pip._internal.cli import main as pip
    pip.main(['install','-U','go-extension'])
    from go_extension import GoExtension

ext = GoExtension(
    name='py_pkg.go',
    pakcages=['example.com/foo/bar/go_pkg'],
    sources=[file.as_posix() for file in Path('go_pkg').glob('*.go') if file.is_file()]
)

setup(
    ext_modules=[ext]
)

Third, build the extension by running:

$ python setup.py build_ext --inplace

Now you can import the Go package as a Python module like:

from py_pkg.go import hello
hello.do_something()
project-root 
│
├─go_pkg
│   └─main.go
│
├─py_pkg
│   ├─go
│   │  ...
│   └─__init__.py
│   
├─go.mod
└─setup.py

Distributions

Go is NOT required to run the compiled Python bindings.

You can build a wheel as a distribution by running:

$ pip install -U wheel
$ python setup.py bdist_wheel

Options

If your go command is not go, then inherit go_extension.build_ext.build_ext_go and change go_command property.

from setuptools import setup
from go_extension.build_ext import build_ext_go

class build_ext(build_ext_go):
    go_command = 'path/to/go'

setup(
    cmdclass={'build_ext': build_ext}
)

License

MIT 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

go-extension-0.0.1.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file go-extension-0.0.1.tar.gz.

File metadata

  • Download URL: go-extension-0.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for go-extension-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ac3e7c227fb361914056c2f4860ad9433a9ad7fbc3c985fdd488e8df211080cb
MD5 5a20a23ca7c00ec836d0f42dd4044796
BLAKE2b-256 40c6e5113d38592216228abb8018a6efd163a0b05ed9a2eb183e593b25e71a38

See more details on using hashes here.

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