Python packaging exe and other efficient tools Nuitka

Python packaging, Nuitka, efficient

Python packaging exe and other efficient tools Nuitka

Preface

Commonly used packaging tools for Python include Nuitka, Pyinstaller, py2exe, and Cx_freeze. The most popular ones are Pyinstaller and Nuitka. In comparison, Nuitka is better and faster. Because:
The function of nuitka is to convert the python program into an executable elf file in C language. In this way, you can enjoy the optimization of C language processing during runtime and improve the speed. After testing, the exe packaged by Nuitka runs 30% faster than the exe packaged by Pyinstaller.

1. Installation

Nuitka can be installed with just one command

pip install nuitka

But in order to make environment dependencies concise, it is better to create a virtual environment

conda create -n EXE python=3.7
conda activate EXE
pip install nuitka

enter

nuitka3 --version
python -m nuitka --version

You can check its version number. The author nuitka installed version 1.2, and the environment used is Ubuntu 20.04, Python3.7

2. Prepare the code

main.py

def say(info):
    return "Say " + info

def main():
    print(say("Hello World"))

if __name__ == "__main__":
    main()

3. Packing

1)Linux

# 打包成文件夹
nuitka3 --standalone --remove-output main.py  # --standalone参数: --standalone 独立环境,这是必须的(否则拷给别人无法使用)
#--remove-output参数:删除临时缓存
# 或 python -m nuitka --standalone --remove-output main.py  (下同)

# 打包成单文件
nuitka3 --standalone --remove-output --onefile main.py  # --onefile 参数: 生成一个打包文件

# 打包成exe
nuitka3 --standalone --remove-output --onefile -o main.exe main.py # -o 指定输出

Package under Linux to generate main.bin or main.exe binary file directly

./main.bin
./main.exe

Ready to run!

2)Win

# 打包成文件夹
nuitka --standalone --remove-output --mingw64 main.py
# 打包成单文件
nuitka3 --standalone --remove-output --mingw64 --onefile main.py

In addition, installing zstandard before packaging can make the packaged files smaller:

pip install zstandard

After installation, the above packaging commands remain unchanged!

3) Error reporting and resolution

Since this is a newly created Python virtual environment, errors and warnings about missing dependent packages may appear during packaging.
(1) Error reporting

Nuitka-Options:INFO: Used command line options: --standalone --remove-output --onefile main.py

Insert image description hereIf it is Ubuntu, directly

apt install patchelf

That’s it!

(2) Report warning

Nuitka:WARNING: Using very slow fallback for ordered sets, please install 'orderedset' PyPI package for best Python
Nuitka:WARNING: compile time performance.

Insert image description herethen directly

pip install orderedset

That’s it!

4) Other packaging parameters

win:

--mingw64 #默认为已经安装的vs2017去编译,否则就按指定的比如mingw(官方建议)

--standalone 独立环境,这是必须的(否则拷给别人无法使用)

--windows-disable-console 没有CMD控制窗口

--output-dir=out 生成exe到out文件夹下面去

--show-progress 显示编译的进度,很直观

--show-memory 显示内存的占用

--include-qt-plugins=sensible,styles 打包后PyQt的样式就不会变了
--plugin-enable=qt-plugins 需要加载的PyQt插件

--plugin-enable=tk-inter 打包tkinter模块的刚需

--plugin-enable=numpy 打包numpy,pandas,matplotlib模块的刚需

--plugin-enable=torch 打包pytorch的刚需

--plugin-enable=tensorflow 打包tensorflow的刚需

--windows-icon-from-ico=你的.ico 软件的图标

--windows-company-name=Windows下软件公司信息

--windows-product-name=Windows下软件名称

--windows-file-version=Windows下软件的信息

--windows-product-version=Windows下软件的产品信息

--windows-file-description=Windows下软件的作用描述

--windows-uac-admin=Windows下用户可以使用管理员权限来安装

--linux-onefile-icon=Linux下的图标位置

--onefile 像pyinstaller一样打包成单个exe文件

--include-package=复制比如numpy,PyQt5 这些带文件夹的叫包或者轮子

--include-module=复制比如when.py 这些以.py结尾的叫模块

Guess you like

Origin blog.csdn.net/Acegem/article/details/127824944