【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.py
  tree = 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.py
  tree = 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.py
  tree = 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 bye

print_hi("K.D.")
hello("LeBron")
bye("Kobe")

 运行

Hi, K.D.
hello LeBron
bye Kobe

Process finished with exit code 0

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

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

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

待续。。

 

 

 

相关推荐

  1. python打包成exe文件

    2024-03-10 17:38:05       62 阅读
  2. python打包成exe文件

    2024-03-10 17:38:05       38 阅读
  3. jupyter转换python文件

    2024-03-10 17:38:05       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 17:38:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 17:38:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 17:38:05       82 阅读
  4. Python语言-面向对象

    2024-03-10 17:38:05       91 阅读

热门阅读

  1. 基于nodejs,使用playwright对网站进行爬虫

    2024-03-10 17:38:05       47 阅读
  2. Qt对话框介绍

    2024-03-10 17:38:05       40 阅读
  3. 【MacOS 上安装 Homebrew 】讲解

    2024-03-10 17:38:05       45 阅读
  4. Kubernetes(K8s)的架构与实现

    2024-03-10 17:38:05       46 阅读
  5. vue2和vue3

    2024-03-10 17:38:05       37 阅读
  6. 微信小程序返回上一页刷新组件数据

    2024-03-10 17:38:05       44 阅读
  7. (科目三)数据库基础知识

    2024-03-10 17:38:05       52 阅读
  8. MySQL用户创建和权限分配

    2024-03-10 17:38:05       45 阅读
  9. uniapp的扩展组件uni-popup 弹出层自动打开

    2024-03-10 17:38:05       43 阅读
  10. 秒杀的时候怎么使用Redis?

    2024-03-10 17:38:05       39 阅读
  11. 第二十六章 :Docker 内部 DNS 服务如何使用

    2024-03-10 17:38:05       38 阅读
  12. 智慧路灯物联网解决方案

    2024-03-10 17:38:05       49 阅读
  13. 深入理解nginx负载均衡round-robin算法

    2024-03-10 17:38:05       41 阅读