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")
相关推荐
李妍.3 小时前
02Numpy基础(上)
开发语言·python
TheBestRucy3 小时前
Python 九阳神功之贰:面向对象(下)
开发语言·python
2601_965958464 小时前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python
AI_小站4 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
微小冷5 小时前
在不同空间中展示数据(欧式、球面、庞加莱圆盘)
python·双曲空间·微分几何·geomstats·庞加莱圆盘·球面
felixking5 小时前
C++20 协程
开发语言·c++·协程
weixin_431600445 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
讲温控就好了5 小时前
从医疗影像到能源存储——芯片冷却温控技术的跨行业赋能
人工智能·python·能源
DeepVisionary6 小时前
从 Qwen3.8-27B 把原生多模态、原生 FP8、桌面级 Agent 三件事一次集齐,看开源大模型的“工业化拐点“
python·自动化
Zane1994126 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言