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")
相关推荐
William_cl17 小时前
C# ASP.NET路由系统全解析:传统路由 vs 属性路由,避坑 + 实战一网打尽
开发语言·c#·asp.net
一起养小猫19 小时前
Flutter for OpenHarmony 实战:打造天气预报应用
开发语言·网络·jvm·数据库·flutter·harmonyos
xyq202419 小时前
Java 抽象类
开发语言
爱装代码的小瓶子19 小时前
【c++与Linux基础】文件篇(4)虚拟文件系统VFS
linux·开发语言·c++
共享家95271 天前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
疯狂的喵1 天前
C++编译期多态实现
开发语言·c++·算法
2301_765703141 天前
C++中的协程编程
开发语言·c++·算法
m0_748708051 天前
实时数据压缩库
开发语言·c++·算法
Hgfdsaqwr1 天前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
lly2024061 天前
jQuery Mobile 表格
开发语言