使用pyinstaller将.py 转换为 .exe
2018年11月4日 2018年11月15日
参考官方文档:https://pyinstaller.readthedocs.io/en/stable/
1 2 3 4 5 6 7 8 9 10 11 |
#安装: pip3 install pyinstaller #然后进入到myfile.py目录下执行以下命令 #转换为单个exe文件: pyinstaller -F myfile.py #-w 表示去掉控制台窗口: pyinstaller -w myfile.py #-i 表示可执行文件的图标: pyinstaller -i my.ico myfile.py #转换为带图标且不需要控制台窗口的程序: pyinstaller -i my.ico -w myfile.py |
以上命令执行后会在目录下生成一个’dist’文件夹
可执行文件就在此目录 下,但并没有版本信息
给打包的程序添加版本信息
1 2 3 |
#添加版本信息需要指定一个版本信息文件 #通过以下命令可以获取到指定可执行程序的版本信息文件"file_version_info.txt" python "*\grab_version.py" *\QQ.exe |
“grab_version.py”文件位置在PyInstaller目录的“utils\cliutils\grab_version.py”
“file_version_info.txt”文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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=(9, 0, 4, 23766), prodvers=(9, 0, 4, 23766), # Contains a bitmask that specifies the valid bits 'flags'r mask=0x3f, # Contains a bitmask that specifies the Boolean attributes of the file. flags=0x0, # 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=0x1, # 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'080404b0', [StringStruct(u'CompanyName', u'Tencent'), StringStruct(u'FileDescription', u'腾讯QQ'), StringStruct(u'FileVersion', u'9.0.4.23766'), StringStruct(u'LegalCopyright', u'Copyright (C) 1999-2018 Tencent. All Rights Reserved'), StringStruct(u'ProductName', u'腾讯QQ'), StringStruct(u'ProductVersion', u'9.0.4.23766')]) ]), VarFileInfo([VarStruct(u'Translation', [2052, 1200])]) ] ) |
修改”file_version_info.txt”文件内容对应我们的程序如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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=(1, 1, 1, 11111), prodvers=(1, 1, 1, 11111), # Contains a bitmask that specifies the valid bits 'flags'r mask=0x3f, # Contains a bitmask that specifies the Boolean attributes of the file. flags=0x0, # 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=0x1, # 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'080404b0', [StringStruct(u'CompanyName', u'GG'), StringStruct(u'FileDescription', u'godgou.com'), StringStruct(u'FileVersion', u'1, 1, 1, 11111'), StringStruct(u'LegalCopyright', u'Copyright (C) 1999-now GG. All Rights Reserved'), StringStruct(u'ProductName', u'gg_face_recognition'), StringStruct(u'ProductVersion', u'1, 1, 1, 11111')]) ]), VarFileInfo([VarStruct(u'Translation', [2052, 1200])]) ] ) |
1 2 3 |
#然后执行: pyinstaller --version-file=file_version_info.txt -i my.ico -w myfile.py #这时查看我们生成后的可执行程序的属性发现版本信息已更新: |