Article Directory
About PyInstaller
Package the packages and interpreters needed for the program to run.
Official documentation: https://pyinstaller.readthedocs.io/en/stable/requirements.html
Package into a folder
pyinstaller main.py
Package into an executable file
A temporary folder is created at runtime, and the required packages are copied to the temporary folder, so it is a little slower. It will be automatically deleted when it ends normally, and the temporary folder may not be deleted when it ends abnormally.
pyinstaller --onefile main.py # pyinstaller -F main.py
pyinstaller --onefile --windowed main.py # 窗口应用
Hide source code
Packaged applications do not contain any source code, but compiled into pyc files can be decompiled to expose code logic.
If you want to hide the source code more thoroughly, compile it with CPython and package it with pyinstaller.
pyinstaller can also encrypt Python bytecode, but it can also be easily decrypted by the key to get the bytecode.
how to use
Execute pyinstaller main.py
, automatically create two directories in the current directory build
and dist
generate a main.spec
file. It contains build
the build logs and files needed during the duild process, dist
and the packaged packages and generated executable files. main.spec
I will talk about it later.
UPX
UPX compresses executable files and libraries, making it smaller. Dynamic decompression during runtime.
Encrypt python bytecode
Need to install PyCrypto module first, use parameters--key={16char}
Multiple versions
pyinstaller
Support different python versions in virtual environment .
pyinstaller
Support different OS under different virtual machines .
sepc file
You can use the pyi-makespec main.py
command to generate the spec file without packaging it for now.
The longest need to modify is datas
this part, you can package the files used in the code together, for example datas=[('./data/1.img', './data/'), ('./model/detect.weights', '.')]
, the format is (relative to the path of spec, relative to the path of executable exe). But it seems to be copied manually after packaging.
Maximum recursion depth exceeded:
The recursion layer is too deep, which can be solved by adding the following two lines of code in the main.spec file
# -*- mode: python -*- import sys sys.setrecursionlimit(5000) #5000可根据情况修改
Cannot find existing PyQt5 plugin directories:
Can't find PyQt5. In this case, you may use conda to install PyQt5. Reinstalling with pip can solve this problem.
You may load I/O plugins with the skimage.io.use_plugin
command. A list of all available plugins can be found using skimage.io.plugins()
:
Modify the relevant places in main.spec to the following form:
from PyInstaller.utils.hooks import collect_data_files, collect_submodules datas = collect_data_files("skimage.io._plugins") hiddenimports = collect_submodules('skimage.io._plugins')
In the actual packaging process, due to the different packages used in the code, you may encounter other problems, you need to explore it yourself.
reference