Moonbit-Python is the first CPython-based bridge for Moonbit language, offering:
- Python Ecosystem Integration - Directly interoperate with top Python libraries like Numpy, Matplotlib, and PyTorch
- Type-Safe Interactions - Strongly-typed interfaces ensuring safe object handling
Requires Python 3.9+. Recommended installation methods:
Linux (Debian/Ubuntu)
sudo apt-get update && sudo apt-get install python3.13 python3.13-dev
macOS (Homebrew)
brew install python@3.13
Windows
- Visit Python Official Download Page
- Download latest 3.x installer
- Enable "Add Python to PATH" during installation
# Verify Python version
python3 --version
# Locate development headers
python3-config --prefix
Update package index and install core library:
moon update
moon add Kaida-Amethyst/python
💡 Note: Current package manager has known issues with native-only libraries. Ignore related error messages. Track official fixes at Moonbitlang
Add to your project's moon.pkg.json
:
{
"import": [
"Kaida-Amethyst/python"
],
"link": {
"native": {
"cc": "$CC",
"cc-flags": "$CC_FLAGS",
"cc-link-flags": "$CC_LINK_FLAGS"
}
}
}
Linux/macOS (env.sh
):
#!/bin/bash
export PY_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
export CC=clang # Recommended for better performance
export CC_FLAGS="-I$(python3-config --prefix)/include/python$PY_VERSION -O2 -DNDEBUG"
export CC_LINK_FLAGS="$(python3-config --ldflags) -lpython$PY_VERSION"
export C_INCLUDE_PATH="$(python3-config --prefix)/include/python$PY_VERSION:$C_INCLUDE_PATH"
Windows PowerShell (env.ps1
):
$PY_PATH = (python -c "import sys; print(sys.prefix)") | Out-String
$env:PY_VERSION = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
$env:CC = "clang"
$env:CC_FLAGS = "-I$($PY_PATH.Trim())\include -O2 -DNDEBUG"
$env:CC_LINK_FLAGS = "-L$($PY_PATH.Trim())\libs -lpython$env:PY_VERSION"
typealias PyInteger = @python.PyInteger
typealias PyList = @python.PyList
typealias PyTuple = @python.PyTuple
fn main {
// It's equivalent to `nums = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]`
let nums = [1L, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
let py_nums = nums.map(PyInteger::from) |> PyList::from
println(py_nums) // Output: [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
// It's equivalent to `import collections`
guard @python.pyimport("collections") is Some(collections)
// It's equivalent to `from collections import Counter`
guard collections.get_attr("Counter") is Some(PyCallable(counter))
let args = PyTuple::new(1)
args.. set(0, py_nums)
// It's equivalent to `cnt = Counter(nums)`
let cnt = counter.invoke(args~).unwrap()
guard cnt is @python.PyDict(cnt)
// `print(cnt)`
println(cnt) // Output: Counter({4: 4, 3: 3, 2: 2, 1: 2})
}
Equivalent Python implementation:
from collections import Counter
l = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
print(Counter(l)) # Counter({4: 4, 3: 3, 2: 2, 1: 2})
We welcome contributions through:
- Issue reporting
- Pull requests
- Ecosystem documentation improvements
📜 License: Apache-2.0 License (See LICENSE file)
Moonbit-Python 是首个基于CPython的Moonbit语言桥接工具,具有以下核心优势:
- 无缝调用Python生态 - 直接操作Numpy、Matplotlib、PyTorch等顶级Python库
- 类型安全交互 - 提供强类型接口保障与Python对象的安全交互
要求Python 3.9+版本,推荐使用最新稳定版:
Linux (Debian/Ubuntu)
sudo apt-get update && sudo apt-get install python3.13 python3.13-dev
macOS (Homebrew)
brew install python@3.13
Windows
- 访问Python官方网站
- 下载最新3.x版本安装包
- 安装时勾选 "Add Python to PATH"
# 验证Python版本
python3 --version
# 获取Python开发头文件路径
python3-config --prefix
更新包索引并安装核心库:
moon update
moon add Kaida-Amethyst/python
💡 注意:当前包管理器对纯Native库的支持存在已知问题,可忽略相关错误提示。官方修复进度请关注Moonbitlang
在项目根目录的 moon.pkg.json
中添加:
{
"import": [
"Kaida-Amethyst/python"
],
"link": {
"native": {
"cc": "$CC",
"cc-flags": "$CC_FLAGS",
"cc-link-flags": "$CC_LINK_FLAGS"
}
}
}
Linux/macOS (env.sh
):
#!/bin/bash
export PY_VERSION=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
export CC=clang # 推荐使用clang以获得更好性能
export CC_FLAGS="-I$(python3-config --prefix)/include/python$PY_VERSION -O2 -DNDEBUG"
export CC_LINK_FLAGS="$(python3-config --ldflags) -lpython$PY_VERSION"
export C_INCLUDE_PATH="$(python3-config --prefix)/include/python$PY_VERSION:$C_INCLUDE_PATH"
Windows PowerShell (env.ps1
):
$PY_PATH = (python -c "import sys; print(sys.prefix)") | Out-String
$env:PY_VERSION = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"
$env:CC = "clang"
$env:CC_FLAGS = "-I$($PY_PATH.Trim())\include -O2 -DNDEBUG"
$env:CC_LINK_FLAGS = "-L$($PY_PATH.Trim())\libs -lpython$env:PY_VERSION"
一个使用python 中Counter的例子
typealias PyInteger = @python.PyInteger
typealias PyList = @python.PyList
typealias PyTuple = @python.PyTuple
fn main {
// It's equivalent to `nums = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]`
let nums = [1L, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
let py_nums = nums.map(PyInteger::from) |> PyList::from
println(py_nums) // Output: [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
// It's equivalent to `import collections`
guard @python.pyimport("collections") is Some(collections)
// It's equivalent to `from collections import Counter`
guard collections.get_attr("Counter") is Some(PyCallable(counter))
let args = PyTuple::new(1)
args.. set(0, py_nums)
// It's equivalent to `cnt = Counter(nums)`
let cnt = counter.invoke(args~).unwrap()
guard cnt is @python.PyDict(cnt)
// `print(cnt)`
println(cnt) // Output: Counter({4: 4, 3: 3, 2: 2, 1: 2})
}
等效Python代码:
from collections import Counter
l = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
print(l) # [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
cnt = Counter(l)
print(cnt) # Out: Counter({1: 2, 2: 2, 3: 3, 4: 4})
我们欢迎任何形式的贡献,包括但不限于:
- 提交Issue报告问题
- 发起Pull Request改进代码
- 编写生态库扩展文档
📜 许可证:Apache-2.0 License(详见LICENSE文件)