https://github.com/sbinet/go-python
naive go bindings to the CPython2 C-API
https://github.com/sbinet/go-python
cgo go golang python2
Last synced: about 2 months ago
JSON representation
naive go bindings to the CPython2 C-API
- Host: GitHub
- URL: https://github.com/sbinet/go-python
- Owner: sbinet
- License: other
- Archived: true
- Created: 2012-07-09T15:43:31.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2023-07-24T07:11:30.000Z (over 2 years ago)
- Last Synced: 2025-04-03T22:42:02.350Z (8 months ago)
- Topics: cgo, go, golang, python2
- Language: Go
- Homepage:
- Size: 188 KB
- Stars: 1,529
- Watchers: 48
- Forks: 139
- Open Issues: 27
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Search and Analytic Databases)
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Search and Analytic Databases)
- awesome-golang-repositories - go-python - API (Repositories)
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go-plus - go-python - naive go bindings to the CPython C-API.  (Embeddable Scripting Languages / Search and Analytic Databases)
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Search and Analytic Databases)
- fucking-awesome-go - :octocat: go-python - naive go bindings to the CPython C-API :star: 377 :fork_and_knife: 37 (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go - go-python - API. | - | - | - | (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go-cn - go-python - API 的 Go 接口。 [![godoc][D]](https://godoc.org/github.com/sbinet/go-python) [![归档项目][Archived]](https://github.com/sbinet/go-python) (可嵌入的脚本语言 / 检索及分析资料库)
- awesome-go - go-python - naive go bindings to the CPython C-API. Stars:`1.5K`. (Embeddable Scripting Languages / Search and Analytic Databases)
- awesome-go - go-python - naive go bindings to the CPython C-API. - :arrow_down:36 - :star:411 (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go - go-python - naive go bindings to the CPython C-API - ★ 759 (Embeddable Scripting Languages)
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Search and Analytic Databases)
- awesome-go - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-Char - go-python - naive go bindings to the CPython C-API. (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go-cn - go-python - API 的 Go 接口。 [![godoc][D]](https://godoc.org/github.com/sbinet/go-python) [![归档项目][Archived]](https://github.com/sbinet/go-python) (可嵌入的脚本语言 / 检索及分析资料库)
- awesome-go-cn - go-python - API` 的` Go` 语言接口 (脚本语言与嵌入式编程 / SQL 查询语句构建库)
- awesome-go-extra - go-python - API|1410|138|28|2012-07-09T15:43:31Z|2021-04-14T08:55:37Z| (Embeddable Scripting Languages / Advanced Console UIs)
- awesome-go - go-python - naive go bindings to the CPython C-API. (<span id="嵌入式脚本语言-embeddable-scripting-languages">嵌入式脚本语言 Embeddable Scripting Languages</span> / <span id="高级控制台用户界面-advanced-console-uis">高级控制台用户界面 Advanced Console UIs</span>)
- awesome-go-cn - go-python - API。 (naive go bindings to the CPython C-API.) (Embeddable Scripting Languages / Advanced Console UIs)
README
go-python
=========
**`sbinet/go-python` only supports CPython2. CPython2 isn't supported anymore by [python.org](https://python.org). Thus, `sbinet/go-python` is now archived.**
**A possible alternative may be to use and contribute to [go-python/cpy3](https://github.com/go-python/cpy3) instead.**
[](https://travis-ci.org/sbinet/go-python)
[](https://ci.appveyor.com/project/sbinet/go-python/branch/master)
[](https://godocs.io/github.com/sbinet/go-python)
Naive `go` bindings towards the C-API of CPython-2.
this package provides a ``go`` package named "python" under which most of the ``PyXYZ`` functions and macros of the public C-API of CPython have been exposed.
theoretically, you should be able to just look at:
http://docs.python.org/c-api/index.html
and know what to type in your ``go`` program.
this package also provides an executable "go-python" which just loads "python" and then call ``python.Py_Main(os.Args)``.
the rational being that under such an executable, ``go`` based extensions for C-Python would be easier to implement (as this usually means calling into ``go`` from ``C`` through some rather convoluted functions hops)
## Install
With `Go 1` and the ``go`` tool, ``cgo`` packages can't pass anymore
additional ``CGO_CFLAGS`` from external programs (except `pkg-config`)
to the "fake" ``#cgo`` preprocessor directive.
``go-python`` now uses ``pkg-config`` to get the correct location of
headers and libraries.
Unfortunately, the naming convention for the ``pkg-config`` package is
not standardised across distributions and OSes, so you may have to
edit the ``cgoflags.go`` file accordingly.
```sh
$ go get github.com/sbinet/go-python
```
If ``go get`` + ``pkg-config`` failed:
```sh
$ cd go-python
$ edit cgoflags.go
$ make VERBOSE=1
```
*Note*: you'll need the proper header and `python` development environment. On Debian, you'll need to install the ``python-all-dev`` package
Documentation
-------------
Is available on ``godocs``:
https://godocs.io/github.com/sbinet/go-python
Example:
--------
```go
package main
import "fmt"
import "github.com/sbinet/go-python"
func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}
func main() {
gostr := "foo"
pystr := python.PyString_FromString(gostr)
str := python.PyString_AsString(pystr)
fmt.Println("hello [", str, "]")
}
```
```sh
$ go run ./main.go
hello [ foo ]
```
TODO:
-----
- fix handling of integers (I did a poor job at making sure everything was ok)
- add CPython unit-tests
- do not expose ``C.FILE`` pointer and replace it with ``os.File`` in "go-python" API
- provide an easy way to extend go-python with ``go`` based extensions
- think about the need (or not) to translate CPython exceptions into go panic/recover mechanism
- use SWIG to automatically wrap the whole CPython api ?