在这篇文章中,将演示如何使用pyi-grab_version工具从应用程序中提取版本信息,以及如何使用pyi-set_version工具为应用程序添加版本信息;

文件的版本信息

Windows 应用可能包含版本信息,用于描述可执行文件的属性:

文件的版本信息
文件的版本信息

默认情况下,使用 PyInstaller 打包生成的文件不包含这类信息;

pyi-grab_version 用法

pyi-grab_version工具可以从任何包含版本信息的 Windows 可执行文件中提取信息;语法如下:

pyi-grab_version exe-file [out-filename]
  • exe-file:包含版本信息的可执行文件路径;如果该文件不包含版本信息,将报错;
  • out-filename:将信息保存到此文件,默认值file_version_info.txt

这是一份从python.exe文件中提取的版本信息:

# 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=(3, 11, 5150, 1013),
    prodvers=(3, 11, 5150, 1013),
    # 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=0x4,
    # 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(
        '000004b0',
        [StringStruct('CompanyName', 'Python Software Foundation'),
        StringStruct('FileDescription', 'Python'),
        StringStruct('FileVersion', '3.11.5'),
        StringStruct('InternalName', 'Python Console'),
        StringStruct('LegalCopyright', 'Copyright © 2001-2023 Python Software Foundation. Copyright © 2000 BeOpen.com. Copyright © 1995-2001 CNRI. Copyright © 1991-1995 SMC.'),
        StringStruct('OriginalFilename', 'python.exe'),
        StringStruct('ProductName', 'Python'),
        StringStruct('ProductVersion', '3.11.5')])
      ]), 
    VarFileInfo([VarStruct('Translation', [0, 1200])])
  ]
)

你可以直接编译该信息以适用你的程序;文件中可能包含 Unicode 字符,请使用utf-8编码编辑和保存;

现在你可以在使用pyinstaller时,添加--version-file选项将版本资源捆绑到程序,示例:

pyinstaller -F --version-file file_version_info.txt main.py

也可在生成程序后使用pyi-set_version命令为程序添加版本信息;

pyi-set_version 用法

pyi-set_version可以将版本信息添加到应用程序中;语法如下:

pyi-set_version info-file exe-file

pyi-set_version可对所有 exe/dll 文件使用;

如果文件已包含版本信息,将会被更新;

  • info-file:包含版本信息的文本文件
  • exe-file:要添加版本信息的可执行文件