程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed operation guide for packaging Python into exe file

編輯:Python

Catalog

Preface

One 、 install pyinstaller

1.1 install pyinstaller, Use the install command :

1.2 If you need to update the version, please enter :

1.3 Check for correct installation

1.4 wait a moment , Water down ;

Two 、python Pack it up exe file ( Packaged with icons )

2.1 The first one is :cd Package and store in the specified folder

2.2 The second method :cd To py Package and store in the source folder

2.3 Final effect

Reference resources : Command interpretation

summary

Preface

I was learning C\C++, This is my first article about python The article . Please take good care of me !

about python Why pack it up as exe file , The reason is that it is necessary to configure a certain environment before compiling to transfer source files and source codes to others , And packaged into exe Files can be opened and run like computer software, and can also be shared with others . But packed exe It is not very convenient to unpack the files , Therefore, you must test before packaging and release after packaging .

For version control, try to choose python3.6+32 Bit version , because win64 Bit system down compatible 32 Bit program , If you don't think about 32 It doesn't matter if it's a bit system , direct python64 Bit version can be packaged directly , But only in win64 Bit system running .

Well, I'll start with the installation pyinstaller Begin to introduce , Then there is the package tutorial , Finally, the command explanation .

PS: This tutorial , We don't have to cmd The little black window of command ;
This tutorial uses :Windows PowerShell(X86)
Remember to use : Administrator run
Pictures are best used ico Suffix picture , Or download a format conversion . Convert and use by yourself

One 、 install pyinstaller1.1 install pyinstaller, Use the install command :pip3 install pyinstaller

1.2 If you need to update the version, please enter :pip install --upgrade pyinstaller1.3 Check for correct installation

emmmm It seems that the installation is complete ,

1.4 wait a moment , Water down ;

python There are many packaging tools , Recommended pyinstaller, This tool supports window and linux Next use . stay windows Next , The size is dozens kb The source file , Pack it up exe Files can be tens of megabytes , The reason is that the library files referenced by the program are packaged together .

Two 、python Pack it up exe file ( Packaged with icons )

There are two methods for this packaging , Choose according to your diligence !

2.1 The first one is :cd Package and store in the specified folder

First step : Move to the designated storage exe File folder

cd Folder path example :cd C:\FILE2\python_file

The second step : Just pack it and you're done ( Look at the code )

pyinstaller -F -w -i Path one ( Icon ) Path two (py file ) Example :pyinstaller -F -w -i D:\python_file\BindundunDemo\qt-logo.ico D:\python_file\BindundunDemo\main.py2.2 The second method :cd To py Package and store in the source folder

First step : Move to source file

cd Folder path example :cd C:\FILE2\python

The second step : Just pack it up ( Look at the code )

pyinstaller -F -w -i Icon name . suffix Source file .py Example :pyinstaller -F -w -i qt-logo.ico one.py

2.3 Final effect

dist What is stored in the file is what you generated exe file , Click to use .

thus , The end of this tutorial !

Here is the command explanation , Please taste it carefully .

Reference resources : Command interpretation Full name of the command command command explanation -F–onefile Packaged as a single executable , If your code is all written in one .py file , You can use , If more than one .py file , Not recommended .-D–onedir Pack multiple files , stay dist Many dependent files are generated in , Suitable for writing tool code in the form of framework , In this way, the code is easier to maintain . The default compilation is -D Of -K–tk Include... At deployment time TCL/TK.-a–ascii Code not included . In support of Unicode Of python The version contains all the codes by default .-d–debug produce debug Version of the executable -w–windowed,–noconsole Use Windows Subsystem execution . The command line will not open when the program starts ( Only on Windows It works )-c–nowindowed,–console Use the console subsystem to perform ( Default )( Only right Windows It works )-s–strip Executables and shared libraries will run through strip. Be careful Cygwin Of strip It may make ordinary win32 Dll Can't use -X–upx If there is UPX install ( perform Configure.py Time to detect ), The execution file is compressed (Windows In the system DLL It will be ).-o DIR–out=DIR Appoint spec Directory where files are generated , If not specified , And the current directory is PyInstaller Root directory , Will automatically create one for output (spec And the generated executable ) The catalog of . If not specified , The current directory is not PyInstaller Root directory , It will be output to the current directory .-p DIR–path=DIR Set the import path ( And use PYTHONPATH Similar effect ). You can use path dividers (Windows Use semicolon ,Linux Use a colon ) Division , Specify multiple directories . You can also use multiple -p Parameter to set multiple import paths , Give Way pyinstaller Find the resources needed by the program -i <FILE.ICO>–icon=<FILE.ICO> take file.ico Resources added as executables ( Only right Windows The system effectively ), Change the icon of the program pyinstaller -i ico route xxxxx.py–i <FILE.EXE, N>–icon=<FILE.EXE, N> take file.exe Of the n Icons added as executable resources ( Only right Windows The system effectively )-v FILE–version=FILE take verfile As the version resource of the executable ( Only right Windows The system effectively )-n NAME–name=NAME Optional items ( Produced spec Of ) name . If omitted , The main filename of the first script will be used as the spec Name summary

This is about Python Pack it up exe The article of the detailed operation guide of the document is introduced here , More about Python Pack it up exe Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved