当前位置: 首页 > news >正文

【Ubuntu】将多个python文件打包为.so文件

1.为什么要将python打包为.so文件?

保护源码

2.实战例子

a.安装相应的包
pip install cython

 验证安装是否成功

cython --version
b.实战的文件目录和内容 

hi.py

# This is a sample Python script.# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.def print_hi(name):# Use a breakpoint in the code line below to debug your script.print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.# Press the green button in the gutter to run the script.
if __name__ == '__main__':print_hi('PyCharm')# See PyCharm help at https://www.jetbrains.com/help/pycharm/

hello.py

def hello(name):print("hello " + name)

bye.py

def bye(name):print("bye " + name)

 setup.py

把需要转换的py文件都放进去

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["hi.py", "hello.py", "bye.py"]))

terminal运行命令

python setup.py build_ext

 运行log

Compiling hi.py because it changed.
Compiling hello.py because it changed.
Compiling bye.py because it changed.
[1/3] Cythonizing bye.py
/home/huanglu/anaconda3/envs/segment-system/lib/python3.10/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/huanglu/Desktop/liubin/so-test/bye.pytree = Parsing.p_module(s, pxd, full_module_name)
[2/3] Cythonizing hello.py
/home/huanglu/anaconda3/envs/segment-system/lib/python3.10/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/huanglu/Desktop/liubin/so-test/hello.pytree = Parsing.p_module(s, pxd, full_module_name)
[3/3] Cythonizing hi.py
/home/huanglu/anaconda3/envs/segment-system/lib/python3.10/site-packages/Cython/Compiler/Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/huanglu/Desktop/liubin/so-test/hi.pytree = Parsing.p_module(s, pxd, full_module_name)

 运行结果

 可以发现多了几个.c文件和build文件夹

build文件夹中

 

复制到根目录

 

在根目录里, 写一个demo.py

from hi import print_hi
from hello import hello
from bye import byeprint_hi("K.D.")
hello("LeBron")
bye("Kobe")

 运行

Hi, K.D.
hello LeBron
bye KobeProcess finished with exit code 0

 把hi.py,hello.py,bye.py删除,再执行demo.py,可以得到同样的结果

即  此时已经使用到了.so文件,而不是原来的.py文件

如果.py在不同文件夹中,则在不同文件夹里面执行类似操作。

待续。。

 

 

 

http://www.lryc.cn/news/312589.html

相关文章:

  • FreeRtos自学笔记3-----参考正点原子视频
  • 使用J-Link Commander通过J-LINK以命令的形式来访问ARM通用MCU
  • 19.删除链表的倒数第N个节点
  • 【Linux C | 网络编程】广播概念、UDP实现广播的C语言例子
  • java 面向对象 day3
  • Flink 大数据 学习详情
  • [项目设计] 从零实现的高并发内存池(四)
  • 02.URL的基本知识和使用
  • 人工智能指数报告2023
  • Android如何对应用进行系统签名
  • 【系统安全加固】Centos 设置禁用密码并打开密钥登录
  • 关于我在项目中封装的一些自定义指令
  • react经验11:访问循环渲染的子组件内容
  • Java开发工程师面试题(业务功能)
  • BUUCTF-Misc-百里挑一
  • 【力扣刷题练习】42. 接雨水
  • 鸿蒙实战开发:数据交互【RPC连接】
  • QLC SSD:LDPC纠错算法的优化方案
  • 【Flutter 面试题】main()和runApp()函数在Flutter的作用分别是什么?有什么关系吗?
  • ChatGPT高效提问——说明提示技巧
  • 从零学算法41
  • FPGA高端项目:FPGA基于GS2971的SDI视频接收+OSD动态字符叠加,提供1套工程源码和技术支持
  • UML-类图详解
  • Python 快速获取PDF文件的页数
  • uniapp开发小程序使用x-www-form-urlencoded; charset=UTF-8 编码格式请求案例
  • 酷开科技服务升级,酷开系统给消费者更好的使用体验!
  • 【leetcode热题】单词拆分
  • 【论文阅读】MC:用于语义图像分割的深度卷积网络弱监督和半监督学习
  • 读书·基于RISC-V和FPGA的嵌入式系统设计·第3章
  • 本地项目推送到腾讯云轻量应用服务器教程(并实现本地推送远程自动更新)