PyInstaller packaged exe files to create installation packages Tutorial


Over the past half year, the first complete back-office functions to achieve code, to achieve front-end interface, to achieve integration between the two calls, just thought about the end of the project, only to find that the hardest part is not yet thread solve. ok, the above is not over, we can only say that the completed application package to make a decent exe executable file is considered perfect with the installer!

This paper is divided into two parts:? The first is how to package the project into exe file and second, how to make packaged exe installation package? In addition, this article describes the issues encountered during actual combat and the corresponding solutions.

Experimental environment and tools of the project as follows:
the Python 3.6.5 ( 'bit-64), 3.5 of PyInstaller, the install the scriptable Nullsoft System 3.02.1, VNISEdit.exe

First, the use PyInstaller the written project package

(1) first shows the project file hierarchy used herein:
Here Insert Picture Description
where, main function is the main file.

(2) generating a file corresponding to the main function SPEC file, generating common method SPEC file is shown below;

  1. The main function file into the directory, according to the spec file generate the following code:
E:\18\project>pyi-makespec main.py
  1. New Suffix for the spec file content directly copy the code below, can be modified.

Spec main contents are as follows:

# -*- mode: python ; coding: utf-8 -*-
block_cipher = None

a = Analysis(['main.py','Ui_Func.py','function.py'],
             pathex=['E:\\18\\project'],
             binaries=[],
             datas=[('E:\\18\\project\\image','image'),('E:\\18\\project\\pysphere','pysphere')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='main',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False,icon='E:\\18\\project\\image\\udbs.ico')

Related explained above code as follows:

  • Analysis: the tuple is placed in a plurality of files need to encapsulate the python, python files to be placed in the first list, if the spec files required package python file not in the same directory, the file need in the array python before adding inventory;
  • pathex: write the path environment variable, which is the path of the project needs to be encapsulated;
  • binaries: This placement path list is called the code is running environment or dependence, such as the configuration of OpenCV, tensorflow like; if not call those dependencies, can be run empty list;
  • datas: This list is placed in the path of the code calls runtime data, list data path first placed in quotation marks, the second parameter is the data stored in this resource exe in the folder name;

(3) begin packing operation
first proceeds to where the project file, using tools PyInstaller packaged, specific steps and the corresponding results are shown below:

C:\Users\despacito>e:
E:\>cd 18\project
E:\18\project>pyinstaller -D main.spec -i image\0108.ico -w
183 INFO: PyInstaller: 3.5
184 INFO: Python: 3.6.5
185 INFO: Platform: Windows-10-10.0.18362-SP0
194 INFO: UPX is not available
197 INFO: Extending PYTHONPATH with paths
['E:\\18\\project', 'E:\\18\\project', 'E:\\18\\project', 'E:\\17\\project']
197 INFO: checking Analysis
197 INFO: Building Analysis because Analysis-00.toc is non existent
.......
65142 INFO: Updating resource type 24 name 1 language 0
65147 INFO: Appending archive to EXE E:\18\project\dist\main.exe
65189 INFO: Building EXE from EXE-00.toc completed successfully.

Common pyinstaller command parameters are as follows:

  • D: generating a list (including a plurality of files) as an executable program;
  • w: removing the console window;
  • i: Specifies generated exe icon;
  • F: generating a single executable file, i.e., can be packaged into a separate application exe file. Note that the instruction is case-sensitive. Here it is capitalized; without this parameter is dependent on a tape and various dll files folder.

So far, the observed current project folder more build and dist directory, dist directory contains the generated exe file. In this case, may occur if the direct double-click the exe following questions:
Here Insert Picture Description
occurs because the problem lies not in the same path under the exe and data files, and data files used in the project as 'E: \ 17 \ project \ image', 'E: \ 17 \ project \ pysphere'. The solution is therefore: the exe path and placed in the same data file: the 'E \ 17 \ project'. ok, the login screen out of it.
Here Insert Picture Description

So, the question is, how to make user (only one exe owner) normal use of the application? And the experience of those who make the application feel more formal and standardized? At this point, you need to install the exe made into a program, user must first go through the installation, then click on the application icon for normal use.

Second, the packaged good exe files installation package made into

The project production tools used in the installation package for the NSIS VNISEdit compiler. VNISEdit Editor is a code-based programming tool under the NSIS script. Note the following when using the tool:

  1. After downloading the editor installed, remember to configure the settings, or in the process of post-creation wizard will appear in the programming language choice question blank.
  2. 进行配置设置时,需下载安装NSIS文件夹压缩器,因为进行NSIS VNISEdit编译器配置设置时会用到NSIS文件夹压缩器下的相关文件,具体如下所示:
    (1)打开NSIS VNISEdit编译器,选择“配置”选项或者快捷键“Ctrl+F11”,如下:
    Here Insert Picture Description
    (2) 紧接着进入如下界面,配置页的原始默认界面如下所示:
    Here Insert Picture Description
    配置后的界面如下所示:
    Here Insert Picture Description

即需将路径选项下的编译器和帮助条目更换成自己电脑本地的相关文件,而这两个文件均为NSIS文件夹压缩器下自带的。点击“确定”配置步即完成。

紧接着开始制作,具体步骤如下:
点击“文件”,选择新建脚本向导,具体如下图所示:
Here Insert Picture Description
此时进入如下界面:
Here Insert Picture Description
点击“下一步”,进行安装包的相关选项设置;
Here Insert Picture Description
Here Insert Picture Description
点击“下一步”,该页面选项均为默认;
Here Insert Picture Description
进入应用程序的目录与授权信息设置,该页面需要修改授权文件,LICENSE.TXT文件在VNISEdit编辑器目录中。
Here Insert Picture Description
按照以下图片内容进行相关设置。
Here Insert Picture Description
Here Insert Picture Description
最终导入的所有文件如下所示:
Here Insert Picture Description

该页面均为默认项。
Here Insert Picture Description
该页面也是默认项;
Here Insert Picture Description
进行解除安装程序的设置,可修改解除安装程序图标;
Here Insert Picture Description
Here Insert Picture Description
配置结束可显示如下界面:
Here Insert Picture Description
至此,制作安装包已经完成,进入相应目录下查看,如图:
Here Insert Picture Description
双击开始安装即可。

最后,附上本项目的功能界面以作留念,哈哈哈哈。
Here Insert Picture Description

发布了21 篇原创文章 · 获赞 6 · 访问量 8004

Guess you like

Origin blog.csdn.net/weixin_42128329/article/details/103885028