Nuitka 打包python项目

前言
我的项目是一个基于python11,带pyqt6的UI的多模块项目,入口文件是main.py
Nuitka版本 2.3.9,系统是win11

具体打包方式不赘述,主要记录我遇到的问题
打包时用到的参数

--onefile  # 打包生成一个exe执行文件
--windows-console-mode=disable  # 不显示windows的控制台窗口
--enable-plugins=PLUGIN_NAME  # 使用插件PLUGIN_NAME
--follow-imports  # 编译所有import的库
--windows-icon-from-ico=ICON_PATH  # 指定软件图标
--include-data-dir # 一起打包的数据文件夹(非代码文件,一些配置文件、txt文件等等),会把整个文件夹下的子文件夹也一起打包,但是会忽略dll、bin文件
--include-data-file  # 一起打包的数据文件

遇到问题:

1.python项目使用了pyqt6做了UI界面

使用参数 --enable-plugin=pyqt6
官方解释:

--enable-plugins=PLUGIN_NAME

Enabled plugins. Must be plug-in names. Use '--plugin-
list' to query the full list and exit. Default empty.

python -m nuitka --onefile --windows-disable-console --enable-plugin=pyqt6 --follow-imports main.py

2.如何打包数据文件

项目中除了代码文件,还有一个配置文件夹,想一起打包

使用参数 --include-data-dir="PC_"="PC_"
官网解释:

--include-data-dir=DIRECTORY

Include data files from complete directory in the
distribution. This is recursive. Check '--include-
data-files' with patterns if you want non-recursive
inclusion. An example would be '--include-data-
dir=/path/some_dir=data/some_dir' for plain copy, of
the whole directory. All non-code files are copied, if
you want to use '--noinclude-data-files' option to
remove them. Default empty.

大概意思就是把DIRECTORY一起打包分发,用法是--include-data-dir=/path/some_dir=data/some_dir
第一个/path/some_dir为要打包的文件夹在项目的路径,第二个data/some_dir为打包之后相对于exe执行文件的位置,就是我们要在代码里取数据就用到这个路径

python -m nuitka --onefile --windows-console-mode=disable --enable-plugin=pyqt6 --follow-imports --windows-icon -from-ico=./SRC/UI/icon.ico --include-data-dir="PC_"="PC_" main.py

3.数据文件夹中含有dll、bin文件,使用--include-data-dir,这两个文件会被忽略

使用参数--include-data-file,单独把这两个文件给打包了
具体用法与--include-data-dir一样
python -m nuitka --onefile --windows-console-mode=disable --enable-plugin=pyqt6 --follow-imports --windows-icon-from-ico=./SRC/UI/icon.ico --include-data-dir="PC_"="PC_" --include-data-file="PC_/rev.dll"="PC_/rev.dll" --include-data-file="PC_/TEMP.bin"="PC_/TEMP.bin" main.py

4.打包的数据文件怎么使用

跟exe一起打包的资源文件,在使用的时候会释放到C盘的某个路径下,因此要使用打包的数据文件,就获取exe的释放路径

# python代码
# 打包时的参数是--include-data-dir="PC_"="PC_",表示PC_文件夹会在exe执行文件释放路径的同级目录
    def copy_simu_config_file(self):
      
        src_path = os.path.dirname(sys.executable) # exe执行文件的释放路径,这个就是打包的数据文件夹所在的路径
        dst_path = os.path.abspath('')  # exe文件所在的路径
        # os.path.join(src_path,"PC_") 这个就是一起打包的数据文件夹,可以直接操作该文件夹和里面的文件
        copy_folder(os.path.join(src_path,"PC_"), os.path.join(dst_path,"PC_")) # 操作数据文件,我这里是把文件夹复制出来

5.不兼容win32位windows系统

我的电脑是win10 64位系统,打包好后,给32位电脑使用,提示“此文件的版本与正在运行的windows版本不兼容”

经排查,需要解决问题有:
1、python版本需要安装32位版本
2、pyqt6不支持32位
因此更换环境为 python3.8-32bit,pyqt6库更换为pyside2 5.15.2.1(据说pyside6 6.1.3版本也是支持的,没有实测过)
之后再重新打包,实测成功兼容win7 32bit系统
python -m nuitka --onefile --disable-console --enable-plugin=pyside2 --follow-imports --windows-icon-from-ico=./SRC/UI/icon.ico --include-data-dir="PC_"="PC_" --include-data-file="PC_/rev.dll"="PC_/rev.dll" --include-data-file="PC_/TEMP.bin"="PC_/TEMP.bin" main.py

posted @ 2024-06-26 19:58  丛影HHZ  阅读(16)  评论(0编辑  收藏  举报