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

Python files are packaged as exe executable files [four steps of personal testing]

編輯:Python

One 、 The roughest packaging method

Operation steps

1、 install pip install pyinstaller( Installation address )
2、 Switch to the project path that needs to be packaged cd D:\task\pyqt5
3、 Run the command pyinstaller -F main.py (xxx.py Packaged files )
4、 After successful packaging, the project is added dist file , Enter and click to run the packaged exe Program

matters needing attention :

Pyinstaller Common parameter usage :

  • –distpath : To which directory
    -w: Specify generation GUI Software , That is, do not open the console at runtime
    -c: Open the console at runtime
    -i : Specify the icon of the packaged executable
    –clean: Clean up before building PyInstaller Cache and delete temporary files About what to pack , There are two options :
    -D: Create a single folder package containing executable files , At the same time, there will be a lot of dependencies dll file , This is the default option
    -F: Generate only .exe file , Located in project dist Under the folder

Add details - Change icon

Recommend an icon to get the address , Inside ico Quite a lot :easyicon
Download my favorite ico after , use -i+ico Path to replace the default ico, Run rebuild as Administrator exe Tools ,
The order is pyinstaller -i ico route -w xxxx.py

Two 、 Parameter transfer is required exe

For example

Lin Mou utilization pyqt Write a graphic interface for emotional annotation of pictures GUI, But the number of pictures to be marked is too large , It can only be marked multiple times by time period , However, the number of personnel completing each marking is random , At the next dimension , The new index given should be the last callout end index +1. So you need to pass the index value to exe File corresponding py file , Realize dynamic following .

Solution

1、main.py The following code should appear in the file :
self.index = int(sys.argv[1])
2、 Open the command prompt window cmd, Switch to main.exe Location path , such as cd D:task\1\, Reenter command main.exe 0 #0 Represents emotional annotation from the first picture
3、 The next time you need to pass parameters , repeat 2 The operation of , Reenter command
main.exe 456 #456 From the first 456 Picture begins with

Details

Why argv[1]?
sys.argv A variable is a that contains command line arguments character string list , That is, use the parameters passed to the program from the command line . Example :cmd perform python using_sys.py we are arguments
The output is as follows :

#encoding=utf-8
import sys
a=sys.argv[0]
b=sys.argv[1]
c=sys.argv[2]
c=sys.argv[3]
print("filename:",a)
print("param1:",b)
print("param2:",c)
print("param3:",d)
Output :
('filename:', 'using_sys.py')
('param1:', 'we')
('param2:', 'are')
('param3:', 'arguments')

among ,sys.argv[0] The default indicates the file path of the code itself , Therefore, manually passed parameters need to be from sys.argv[1] Start

3、 ... and 、 It's packaged exe File is too large.

1、 As far as possible with from…import…

Try not to use import, can from…import… Just try to use this , Because if it is import Words , When packing , Will pack the whole package to exe Inside , Meaningless increase exe The size of the tool

2、 Solution

use Pyinstaller pack Python Program + Solve the problem that the packaging result is too large


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