Examples of Pyinstaller packaged EXE files

Source: Internet
Author: User

This is the continuation of Python writing the Windows Tomcat daemon. Before the code is nothing to be packaged into EXE file, Python can be packaged into EXE tools are mainly Py2exe, Pyinstaller, Cx_freeze. Which Py2exe feeling is the most difficult to use, packing the worst; Cx_freeze packaging is simpler, but the function is not two other powerful, so personal feeling pyinstaller is the best use. Pyinstaller Support package compression, packaging into a single file, add personality icon, set copyright information. The effect is as follows:

First, installation

Pyinstaller installation is relatively simple, support PIP installation, directly using the following command to complete the installation:

Pip Install Pyinstaller
Not afraid of trouble can also be downloaded from the source Python setup.py install to install.

It is to be noted that Pyinstaller relies on the Pywin32 module under the Windows platform. Before using Pyinstaller, you need to install the Pywin32 module (EXE package, installation is very simple).

Second, the use

After the default installation is complete, the Pyinstaller program is located in the C:\Python27\Scripts directory and can generate executables by executing Pyinstaller python_script.py. The resulting executable file is located in the Dist directory under the directory where the execution resides. The advanced parameters are as follows:

–distpath=path_to_executable Specifies the directory where the generated executable file is stored, which is stored by default in the Dist directory
–workpath=path_to_work_files specifies the directory in which temporary files are stored in the compilation, which are stored by default in the build directory
–clean Clean Compile-time temporary files
-f,–onefile generate separate EXE files instead of folders
-d,–debug compiled into debug mode to help get log information in the run
–version-file=version_text_file to add version information for EXE file, version information can be generated by running Pyi-grab_version plus the path of EXE file to get version information. The generated version information file can be modified as required and added as a--version-file parameter to the EXE file to be generated
-I <file.ico>-I <FILE.exe,ID>,–icon=<FILE.ico>,–icon=<FILE.exe,ID>
To add an icon to an EXE file, you can specify an icon path or extract a specific id icon from an existing EXE file as an icon for the EXE file to be generated
In addition, you can generate executable files from the spec file. The specific orders are as follows:

Pyinstaller Specfile
Or

Pyi-build Specfile

Note: The spec file will exist every time by the command generation, you can add the corresponding function by the simple modification, such as the icon, specify the copyright file.

As an example of a previously written program, you can generate an executable file by using the following command

C:\USERS\THINKPAD>CD/D C:\Users\thinkpad\Desktop\monitor
C:\users\thinkpad\desktop\monitor>c:\python27\scripts\pyinstaller.exe-f monitor.py

Add an ICO icon file to monitor by following these steps:

C:\users\thinkpad\desktop\monitor>c:\python27\scripts\pyinstaller.exe-i alert.ico-f monitor.py

The additional version information file method is as follows:

C:\users\thinkpad\desktop\monitor>c:\python27\scripts\pyinstaller.exe-i Alert.ico--version-file=file_version _info.txt-f monitor.py
Version of the information to look at the back.

At this time the spec file reads as follows:

#-*-Mode:python-*-
Block_cipher = None
A = Analysis ([' monitor.py '],
pathex=[' C:\\users\\thinkpad\\desktop\\monitor '],
Binaries=none,
Datas=none,
Hiddenimports=[],
Hookspath=[],
Runtime_hooks=[],
Excludes=[],
Win_no_prefer_redirects=false,
Win_private_assemblies=false,
Cipher=block_cipher)
Pyz = Pyz (A.pure, A.zipped_data,
Cipher=block_cipher)
EXE = EXE (PYZ,
A.scripts,
A.binaries,
A.zipfiles,
A.datas,
Name= ' Monitor ',
Debug=false,
Strip=false,
Upx=true,
Console=true, Version-file=file_version_info.txt, icon= ' Alert.ico ')

Third, UPX compression

The default generated Python executable file is a bit large, can be compressed through the UPX program, UPX program's homepage is located in: https://upx.github.io/, the current Windows version of the latest upx for upx391w.zip, download, Place the Upx.exe file in the C:\Python27 directory (the directory where the Python executable program is installed), and then follow the--upx to compress it again. When the default exists, compression is not specified, and when the EXE file is generated, the following contents can be found in its log:

1087 info:pyinstaller:3.2
1087 info:python:2.7.12
1088 Info:platform:windows-7-6.1.7600-sp0
1090 Info:wrote C:\Users\thinkpad\Desktop\monitor\monitor.spec
1172 INFO:UPX is available.
1174 info:extending Pythonpath with paths
[' C:\\users\\thinkpad\\desktop\\monitor ',
' C:\\users\\thinkpad\\desktop\\monitor ']

You can see that the above UPX is available. Through spec file Also, add Upx=true can.

Iv. generation of version information files

You can read an existing EXE file and obtain its version information by grab_version.py, as follows:

C:\users\thinkpad\desktop\monitor>c:\python27\lib\site-packages\pyinstaller\utils\cliutils\grab_ version.py C:\Windows\System32\sc.exe
Version info written to:file_version_info.txt
will File_version_ Info.txt is placed in the corresponding directory and the internal information is modified, you can generate an executable file with copyright information in the method in step 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.