gpython

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 9 Imported by: 0

README

GPython Engine · Python 脚本引擎

Go Version Engine Type

基于 gpython 的纯 Go Python 3.4 子集脚本引擎

中文 · English · 日本語


概述

GPython 引擎使用 go-python/gpython 库,为 Go 应用提供 Python 3.4 子集的脚本执行能力。无需 CPython 依赖,纯 Go 实现,跨平台编译零障碍。

引擎特性
特性 说明
底层库 github.com/go-python/gpython v0.2.0
语言版本 Python 3.4 子集
标准库 内置 systimemath 等原生模块
线程安全 双锁模式(RWMutex + execMutex
热更新 支持 StartWatch / StopWatch
类型常量 scriptEngine.GPythonType

安装

go get github.com/tx7do/go-scripts/gpython

快速开始

package main

import (
    "context"
    "fmt"
    "log"

    scriptEngine "github.com/tx7do/go-scripts"
    _ "github.com/tx7do/go-scripts/gpython" // 注册 GPython 引擎
)

func main() {
    eng, err := scriptEngine.NewScriptEngine(scriptEngine.GPythonType)
    if err != nil {
        log.Fatal(err)
    }
    defer eng.Close()

    ctx := context.Background()
    if err := eng.Init(ctx); err != nil {
        log.Fatal(err)
    }

    // 注入宿主变量
    _ = eng.RegisterGlobal("name", "world")

    // 注册宿主函数
    _ = eng.RegisterFunction("greet", func(name string) string {
        return fmt.Sprintf("Hello, %s!", name)
    })

    // 执行 Python 脚本
    result, _ := eng.ExecuteString(ctx, "hello.py", `greet(name)`)
    fmt.Println(result)
}

API 参考

方法 说明
Init(ctx) 创建 gpython 解释器上下文和主模块
Close() 关闭解释器,释放资源
LoadString(ctx, name, code) 编译 Python 源码并加入执行队列
Execute(ctx) 顺序执行队列中所有脚本
ExecuteString(ctx, name, code) 编译并立即执行内联 Python 代码
RegisterGlobal(name, value) 注入 Go 值为 Python 全局变量
RegisterFunction(name, fn) 注册 Go 函数为 Python 可调用对象
CallFunction(ctx, name, args...) 调用 Python 函数
RegisterModule(name, module) 注册模块(键值映射为 模块名_键名 全局变量)
StartWatch(ctx, key) 启动脚本热更新监听
StopWatch(key) 停止热更新监听

测试

cd gpython && go test -v ./...

相关文档

License

MIT License

Documentation

Overview

Package gpython provides a script engine implementation using the gpython Python 3 interpreter (https://github.com/go-python/gpython). It allows executing Python scripts at runtime in a pure Go environment without CPython dependencies.

Construction:

eng, _ := gpython.New(ctx)
eng.Init(ctx)
eng.LoadString(ctx, "hello", `print("hello world")`)
result, err := eng.Execute(ctx)

The engine supports hot-reload via StartWatch/StopWatch when the bound Source implements the Watcher interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrGPythonEngineNotInitialized is returned when an operation is invoked
	// before Init or after Close.
	ErrGPythonEngineNotInitialized = errors.New("gpython engine not initialized")

	// ErrGPythonEngineAlreadyInitialized is returned when Init is called on an
	// already-initialized engine.
	ErrGPythonEngineAlreadyInitialized = errors.New("gpython engine already initialized")

	// ErrGPythonContextNotInitialized is returned when the underlying py.Context
	// is nil.
	ErrGPythonContextNotInitialized = errors.New("gpython context not initialized")

	// ErrGPythonCompileFailed is returned when compiling Python source fails.
	ErrGPythonCompileFailed = errors.New("gpython compile failed")

	// ErrGPythonNoScriptLoaded is returned when Execute is called but no script
	// has been loaded.
	ErrGPythonNoScriptLoaded = errors.New("gpython no script loaded")

	// ErrGPythonRunFailed is returned when running Python code fails.
	ErrGPythonRunFailed = errors.New("gpython run failed")

	// ErrGPythonFunctionNotFound is returned when CallFunction cannot find the
	// named function.
	ErrGPythonFunctionNotFound = errors.New("gpython function not found")

	// ErrGPythonGlobalNotFound is returned when GetGlobal cannot find the named
	// variable.
	ErrGPythonGlobalNotFound = errors.New("gpython global not found")
)

Sentinel errors returned by the GPython engine.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL