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")
相关推荐
曲幽9 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805113 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon15 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly15 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程15 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly17 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风1 天前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi2 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风2 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python