Use PyInstaller to convert python to executable exe notes

  • 2020-09-16 07:35:52
  • OfStack

1. Install PyInstaller

PyInstaller is for PyInstaller and UPX. UPX is used to compress exe. Click the hyperlink to download. The current stable version is 1.3. For example, under Windows, put the downloaded upx.exe into the uncompressed PyInstaller folder. Set the folder for PyInstaller to D:\PyInstaller, the same below. Enter D:\PyInstaller from the command line, run Configure.py, and you should see the following information:

[

I: computing EXE_dependencies
I: Finding TCL/TK...
I: found TCL/TK version 8.5
I: testing for Zlib...
I: ... Zlib available
I: Testing for ability to set icons, version resources...
I: ... resource update available
I: Testing for Unicode support...
I: ... Unicode available
I: testing for UPX...
I: ...UPX available
I: computing PYZ dependencies...

]

Messages beginning with E(Error) cannot appear, and it is best not to have W(Warning) messages. If an dll cannot be found, please place the dll file under C:\Windows\system32, 1 usually resolves.

Linux users also need to compile Runtime Executables, Windows users do not. It is mainly used to run ES64en.py and generate run and run_d files under PyInstaller\support\load\. See the description of PyInstaller\doc\ Manual.html for details.

2. Write an Py program

For experiment, write 1 HelloWorld.py, assuming save on C:\ HelloWorld.py


#!/usr/bin/env python
print 'Hello,World!'
words = raw_input('What do you want to say? ')
print 'you said:'+words

Create the spec file

The spec file is used to tell PyInstaller which py file to compile and the parameters. Just execute "Makespec. py+ parameters +Py code path". The main parameters are as follows (see PyInstaller\doc\ Manual.html) :

-F, -onefile Py code has only 1 file
-ES108en, --onedir Py code in a directory (default is this)
-ES112en, --tk contains TCL/TK
-ES117en, --debug generates exe file for debug schema
-ES122en, --windowed, --noconsole Forms exe file (Windows Only)
-ES129en, --nowindowed, --console Console exe file (Windows Only)
X, --upx USES upx to compress exe files
-ES141en DIR, --out=DIR Sets the spec file output directory, default in PyInstaller same directory
--icon= < FILE.ICO > Add ICONS (Windows Only)
-ES155en FILE, --version=FILE add version information file

For ES161en.py, execute the following code:

[

Makespec.py --onefile --console --upx --tk -o C:\ C:\HelloWorld.py

]

After execution C:\ HelloWorld.spec

4. Build spec file, generate exe file

Perform:

[

Build.py C:\HelloWorld.spec

]

After a long message, you will find HelloWorld. exe file under C:\ and here it is! The volume is quite large, 2.7M, this is because this HelloWorld program to kill chicken with a knife. Other files are process files and can be deleted.


Related articles: