1. 安裝pyinstaller庫(kù)
python -m pip install pyinstaller
2. 打包ip.py文件
C:\Users\ws\Desktop\crawler\temp > pyinstaller -F -c --icon=my.ico ip.py
62 INFO: PyInstaller: 3.3.1
62 INFO: Python: 3.6.5
62 INFO: Platform: Windows-10-10.0.16299-SP0
......
9148 INFO: Appending archive to EXE
C:\Users\ws\Desktop\crawler\temp\dist\ip.exe
9148 INFO: Building EXE from out00-EXE.toc completed successfully.
我這里要打包的文件是C:\Users\ws\Desktop\crawler\temp\ip.py, 文件圖標(biāo)名為my.ico, 在控制臺(tái)運(yùn)行(沒(méi)做GUI)
pyinstaller里的主要參數(shù):
-icon=圖標(biāo)路徑
-F 打包成一個(gè)exe文件
-w 使用窗口,無(wú)控制臺(tái)(有GUI的程序要用到)
-c 使用控制臺(tái)儒溉,無(wú)窗口
-D 創(chuàng)建一個(gè)目錄送浊,里面包含exe以及其他一些依賴(lài)性文件
–version-file file_version_info.txt :表示將標(biāo)準(zhǔn)版本信息文件的內(nèi)容賦給exe文件的屬性
更多請(qǐng)鍵入 pyinstaller -h 來(lái)查看參數(shù)
dist目錄下是exe文件,可單獨(dú)執(zhí)行。
3. (可選)打包程序時(shí)鍵入版本信息
- 在ip.py所在目錄下按以上示例創(chuàng)建一個(gè)版本信息文件:file_version_info.txt 文本文件潦牛。
我這里給出 file_version_info.txt 的具體格式示例。
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
ffi=FixedFileInfo(
# filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
# Set not needed items to zero 0.
filevers=(0, 1, 0, 0),
prodvers=(1, 1, 0, 0),
# Contains a bitmask that specifies the valid bits 'flags'r
mask=0x3f,
# Contains a bitmask that specifies the Boolean attributes of the file.
flags=0x28,
# The operating system for which this file was designed.
# 0x4 - NT and there is no need to change it.
OS=0x40004,
# The general type of file.
# 0x1 - the file is an application.
fileType=0x2,
# The function of the file.
# 0x0 - the function is not defined for this fileType
subtype=0x0,
# Creation date and time stamp.
date=(0, 0)
),
kids=[
StringFileInfo(
[
StringTable(
u'000004b0',
[StringStruct(u'FileVersion', u'0.1.0.0'),
StringStruct(u'ProductVersion', u'1.1.0.0'),
StringStruct(u'OriginalFilename', u'ip'),
StringStruct(u'InternalName', u'ip'),
StringStruct(u'FileDescription', u'<無(wú)>'),
StringStruct(u'CompanyName', u'<小白王_960064995>'),
StringStruct(u'LegalCopyright', u'?小白王_960064995'),
StringStruct(u'ProductName', u'ip'),
StringStruct(u'Comments', u'<IP地址查詢(xún)>'),
StringStruct(u'LegalTrademarks', u' '),
StringStruct(u'e', u''),
StringStruct(u'ld', u' '),
StringStruct(u'i', u''),
StringStruct(u'ld', u'D')])
]),
VarFileInfo([VarStruct(u'Translation', [0, 1200])])
]
)
- 打開(kāi)系統(tǒng)cmd鍵入:pyinstaller -F -c --icon=my.ico --version-file file_version_info.txt ip.py (注意:重新打包挡育!巴碗,示例)
C:\Users\ws\Desktop\crawler\temp>pyinstaller -F -c --icon=my.ico --version-file file_version_info.txt ip.py
62 INFO: PyInstaller: 3.3.1
62 INFO: Python: 3.6.5
62 INFO: Platform: Windows-10-10.0.16299-SP0
62 INFO: wrote C:\Users\ws\Desktop\crawler\temp\ip.spec
62 INFO: UPX is not available.
78 INFO: Extending PYTHONPATH with paths
['C:\\Users\\ws\\Desktop\\crawler\\temp',
'C:\\Users\\ws\\Desktop\\crawler\\temp']
78 INFO: checking Analysis
78 INFO: Building Analysis because out00-Analysis.toc is non existent
78 INFO: Initializing module dependency graph...
78 INFO: Initializing module graph hooks...
78 INFO: Analyzing base_library.zip ...
......
9146 INFO: Writing RT_ICON 1 resource with 4264 bytes
9177 INFO: Appending archive to EXE C:\Users\ws\Desktop\crawler\temp\dist\ip.exe
9177 INFO: Building EXE from out00-EXE.toc completed successfully.
-
成功!
公眾號(hào):勝言
個(gè)人博客:wangsheng.tech