Python脚本打包 .so & .exe

一、Python程序打包成可执行程序

1.在python脚本的运行环境下安装打包工具 pyinstaller

pip3 install pyinstaller # 基于Linux下的安装

2.找到可执行py脚本路径,开始打包

pyinstaller -i logo.ico -F -c sc.py # 有图标
pyinstaller -F -c sc.py # 无图标

Pyinstaller工具的基本参数如下:

  • -i: 表示要加载的图标(没有选择图标可以不用写)
  • x.ico:表示自己选择的图标名(没有选择图标可以不用写)
  • -F:表示打包成.exe可执行文件
  • -c:表示打包程序有窗口
  • sc.py:表示你要打包的py文件

执行上述指令之后就会在本地目录下的dist目录中找到对应的可执行程序sc.

、Python程序打包.so动态库

1. 安装cpython库等环境

pip3 install cython

2.编写setup.py文件

 1 from distutils.core import setup
 2 from distutils.extension import Extension
 3 from Cython.Build import cythonize
 4 
 5 extensions = [Extension("ln_facedetector",
 6                         ["ln_facedetector.py"],
 7                         include_dirs=["/usr/local/lib"],
 8                         libraries=["/usr/local/lib/python3.6/site-packages/cv2/cv2.cpython-36m-x86_64-linux-gnu.so",
 9                                    "/usr/local/lib/python3.6/site-packages/dlib.cpython-36m-x86_64-linux-gnu.so"],
10                         library_dirs=["/usr/local/lib/python3.6/site-packages/cv2","/usr/local/lib/python3.6/site-packages"])]
11 
12 setup(ext_modules=cythonize(extensions))

上述ln_facedetector为待转换的.so包名称,对应的python脚本文件,整个工程中包括多少个py文件,就写多少个Extension,并把需要调用的其他库路径在include_dirs、libraries、library_dirs中进行设置

3. 编译so文件,执行以下命令

python setup.py build_ext

4. 调用so文件

import sys
sys.path.append('/vloum/faceSO/ln_facedetector') #加载so文件(入参为so文件路径)
from ln_facedetector import faceutil #import so文件中的相关类
arr = faceutil.detection("/vloum/faceSO/a.jpg")
print(arr)

Reference

exe打包:https://blog.csdn.net/zhezhebie/article/details/81908033

so打包:https://www.cnblogs.com/answerThe/p/11600590.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章