{"id":4256,"library":"setuptools-golang","title":"Setuptools Golang Extension","description":"setuptools-golang is a setuptools extension designed for building CPython extensions written in Go. It enables developers to integrate Go-based modules directly into Python packages. The library is currently at version 2.9.0, but its GitHub repository was archived in January 2025, indicating it is no longer actively maintained. The primary maintainer has stated that multiple Go shared objects in a single process are not supported by Go 1.21+ and there is no intention to fix this, severely limiting its utility with modern Go versions.","status":"deprecated","version":"2.9.0","language":"en","source_language":"en","source_url":"https://github.com/asottile/setuptools-golang","tags":["python","go","golang","setuptools","extension","build","cpython"],"install":[{"cmd":"pip install setuptools-golang","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"It's an extension for setuptools.","package":"setuptools"},{"reason":"Requires Go compiler (>= 1.5) to build extensions.","package":"golang","optional":false}],"imports":[{"note":"setuptools-golang is configured via the 'setup()' function in setup.py, specifically through 'setup_requires' and the 'build_golang' dictionary, rather than direct imports of classes from the setuptools_golang package itself.","symbol":"Extension","correct":"from setuptools import Extension"}],"quickstart":{"code":"import os\nfrom setuptools import setup, Extension\n\n# Create a dummy Go source file for the example\nwith open('example.go', 'w') as f:\n    f.write(\"\"\"\npackage main\n\nimport \"C\"\n\n//export Sum\nfunc Sum(a, b int) int {\n\treturn a + b\n}\n\nfunc main() {}\n\"\"\")\n\nsetup(\n    name='my_go_extension',\n    version='0.1.0',\n    description='A Python extension in Go',\n    setup_requires=['setuptools-golang'],\n    build_golang={'root': 'github.com/user/project'},\n    ext_modules=[\n        Extension(\n            'my_go_extension.example',\n            ['example.go'],\n        ),\n    ],\n)\n\n# To demonstrate usage, you would typically run:\n# python setup.py build_ext --inplace\n# Then in Python:\n# import my_go_extension.example\n# print(my_go_extension.example.Sum(1, 2))\n","lang":"python","description":"A `setup.py` demonstrating how to define a Go extension using `setuptools-golang`. The `build_golang` dictionary specifies the root Go import path, and `Extension` points to the Go source file(s). This example creates a simple Go function `Sum` that adds two integers, exposed to Python."},"warnings":[{"fix":"Consider alternative methods for Python-Go interoperability (e.g., RPC, FFI directly, or gopy), or pin your Go version to < 1.21. For new projects, avoid this library.","message":"The project is officially deprecated and archived. The maintainer states that 'multiple go shared objects in a single process is not supported' and 'it likely broke in go 1.21 and there is no intention to fix it'. This means extensions built with setuptools-golang may not work with Go versions 1.21 and newer.","severity":"breaking","affected_versions":"Go >= 1.21, setuptools-golang 2.x"},{"fix":"Add `global-include *.c` to your `MANIFEST.in` file to ensure C source files are included alongside Go sources.","message":"When building extensions that involve C code (e.g., CGo), related C files might not be included in the distribution by default, leading to 'undefined reference' errors during linking.","severity":"gotcha","affected_versions":"All"},{"fix":"Double-check the correctness of your Go import paths. Ensure that all external dependencies are correctly vendored or accessible through the Go module system.","message":"Incorrect or non-existent external Go import paths can lead to 'fatal: could not read Username for 'https://github.com'' or 'package github.com/a/b/c: ... exists but .../.git does not' errors.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that global variables defined in your C code that are accessed by Go are marked with `extern`.","message":"Duplicate symbol errors (e.g., `duplicate symbol _XXX`) can occur if global variables defined in C code are not correctly marked for Go's CGo interaction.","severity":"gotcha","affected_versions":"All"},{"fix":"Set the `SETUPTOOLS_GOLANG_GOPATH` environment variable to reuse a consistent GOPATH, e.g., `$ SETUPTOOLS_GOLANG_GOPATH=~/go pip install .`","message":"Repeated builds can be slow due to Go's default GOPATH management.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}