Use PyInstaller to package python into exe

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 buildand distgenerate a main.specfile. It contains buildthe build logs and files needed during the duild process, distand the packaged packages and generated executable files. main.specI 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

pyinstallerSupport different python versions in virtual environment .

pyinstallerSupport different OS under different virtual machines .

sepc file

You can use the pyi-makespec main.pycommand to generate the spec file without packaging it for now.

The longest need to modify is datasthis 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.

Reference: https://stackoverflow.com/questions/52376313/converting-py-file-to-exe-cannot-find-existing-pyqt5-plugin-directories/52376965



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')

Reference: https://stackoverflow.com/questions/34761862/pyinstaller-you-may-load-io-plugins-with-the-skimage-io-use-plugin



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

Published 74 original articles · Like 11 · Visits 30,000+

Guess you like

Origin blog.csdn.net/yijiull/article/details/89740089