python 打包成 .so

1)生成 .so库文件,使用脚本 py2sp.py,编译目标 python 文件成 .so

py2so

python 复制代码
import Cython.build
import distutils.core

def py2so(file):
    cpy Cython.Build.cythonize(file) # 返回 distuls.extension.Extension 对象列表
    
    distutils.core.setup(
    	name = 'python_to_so',	# 包名称
        version = "1.0",	# 包版本号
        ext_modules = cpy,	# 扩展模块
        author = 'ohuo',	# 作者
        author_email = 'aha',	# 作者邮箱
    )

if __name__ == '__main__':
    file = 'hello.py'
    py2so(file)

2)执行编译脚本

python3 py2so.py build_ext --inplace

3)使用 .so中打包的方法,import_my_so.py

python 复制代码
import hello
hello.main()

输出:

aha, hello

·hello.py·

python 复制代码
def main():
    print("aha, hello")
相关推荐
用户8356290780512 分钟前
使用 Python 设置 Excel 数据验证
后端·python
AIFarmer10 分钟前
【无标题】
开发语言·c++·算法
Nick_zcy16 分钟前
小说在线阅读网站和小说管理系统 · 功能全解析
java·后端·python·springboot·ruoyi
*Lisen22 分钟前
从零手写 FlashAttention(PyTorch实现 + 原理推导)
人工智能·pytorch·python
昇腾CANN24 分钟前
TileLang-Ascend 算子性能优化方法与实操
开发语言·javascript·性能优化·昇腾·cann
沐知全栈开发35 分钟前
ionic 手势事件详解
开发语言
用户83562907805138 分钟前
用 Python 轻松在 Excel 工作表中应用条件格式
后端·python
red1giant_star43 分钟前
Python根据文件后缀统计文件大小、找出文件位置(仿Everything)
后端·python
雷欧力1 小时前
如何使用 Claude API?3 种接入方案实测,附完整代码(2026)
python·claude
lsx2024061 小时前
Bootstrap 按钮
开发语言