关于pyinstaller打包的相关知识记录

1、安装pyinstaller:

直接在pycharm里面打开终端:

pip install pyinstaller

2、打包torch环境下的程序:

pyinstaller xxx.py  // 打包成一个文件夹形式
# pyinstaller -F xxxx.py  //打包成一个单独的exe形式(二选一即可)

pyinstaller打包的相关参数:

-F,-onefile打包成一个单独的exe文件,内部打包的文件不可见
-D,-onedir,无参数打包成一个文件夹,包含多个文件。打包后的文件夹通常包含可执行文件、依赖项以及其他资源文件。

-w,-windowed,

-noconsole

在生成的可执行文件中禁用控制台窗口,打包后的程序不会在运行时显示命令行窗口。

禁用控制台窗口的打包方法:

pyinstaller xxx.py --noconsole
# pyinstaller -F xxx.py --noconsole

记录在打包过程中的错误:

(1)打包之后的exe在运行时报错缺少torch:

       我这里的原因是pyinstaller安装的位置和我的torch不在一起,我是重新新建了一个虚拟环境,用的之前的pyinstaller,在当前环境下再重新安装一下pyinstaller就好了。

(2)onnxruntime报错:

2023-12-26 13:34:51.2112545 [W:onnxruntime:Defaultonnxruntime_pybind_state.cc:1983 onnxruntime::python::CreateInferencePybindStateModule] Init provider bridge failed.

解决:打开打包生成的.spec文件,修改data:

datas=[('D:\\anaconda\\envs\\mypyqt\\Lib\\site-packages\\onnxruntime\\capi\\onnxruntime_providers_shared.dll', 'onnxruntime\\capi')]

然后重新打包.spec:

pyinstaller xxxx.spec

 重新生成的exe就没有问题了。

注:我使用pyinstaller -F xxxx.spec报错

option(s) not allowed:
  --onedir/--onefile
makespec options not valid when a .spec file is given

去掉-F就好了。

参考: Pyinstaller打包onnxruntime、pyqt等出现的问题总结_pyinstaller onnxruntime-CSDN博客()

补充,如果想在打包的时候加入其他的文件,比如图像,pth模型等,在.spec文件里面添加,比如:我现在想加入一张图像test.jpg

a = Analysis(
    ['enter.py'],
    pathex=[],
    binaries=[],
    datas=[('D:\\anaconda\\envs\\mypyqt\\Lib\\site-packages\\onnxruntime\\capi\\onnxruntime_providers_shared.dll', 'onnxruntime\\capi'),('./test.jpg', '.'),('datas','./datas')],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
)

 ('./test.jpg', '.')第一个参数'./test.jpg'表示图像的存放的相对路径,第二个参数 '.'表示将test.jpg复制到打包文件的根目录下。('datas','./datas')表示在打包文件里面新建一个datas文件,将我自己的文件夹datas下的所有文件复制到打包文件下的datas文件里面。

(3)运行exe时报错:

enter.exe: error: unrecognized arguments: --multiprocessing-fork parent_pid=20068 pipe_handle=2212

解决:先在最开始导入multiprocessing

import multiprocessing

然后在if __name__ == '__main__':的后面加上:multiprocessing.freeze_support()

if __name__== '__main__':
    multiprocessing.freeze_support()

参考:2021-04-15_pyinstaller: error: unrecognized arguments: -w-CSDN博客

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值