导航

pyinstaller 时碰到 struct.error argument out of range

Posted on 2023-09-13 13:42  蝈蝈俊  阅读(727)  评论(0编辑  收藏  举报

使用 PyInstaller 来打包 Python 程序,

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

并且在运行打包后的程序时遇到了这个错误:

struct.error argument out of range

它通常表示在使用 Python 的 struct 模块时,传递给 struct 函数的参数超出了其所能处理的范围。

错误

570708 INFO: Copying version information to EXE
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\env\fbt\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "D:\env\fbt\Lib\site-packages\PyInstaller\__main__.py", line 194, in _console_script_run
    run()
  File "D:\env\fbt\Lib\site-packages\PyInstaller\__main__.py", line 180, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "D:\env\fbt\Lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "D:\env\fbt\Lib\site-packages\PyInstaller\building\build_main.py", line 1019, in main
    build(specfile, distpath, workpath, clean_build)
  File "D:\env\fbt\Lib\site-packages\PyInstaller\building\build_main.py", line 944, in build
    exec(code, spec_namespace)
  File "d:\mycode\tanghai\mellifera\MelliferaCMD.spec", line 24, in <module>
    exe = EXE(
          ^^^^
  File "D:\env\fbt\Lib\site-packages\PyInstaller\building\api.py", line 590, in __init__
    self.__postinit__()
  File "D:\env\fbt\Lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
    self.assemble()
  File "D:\env\fbt\Lib\site-packages\PyInstaller\building\api.py", line 694, in assemble
    versioninfo.write_version_info_to_executable(build_name, self.versrsrc)
  File "D:\env\fbt\Lib\site-packages\PyInstaller\utils\win32\versioninfo.py", line 593, in write_version_info_to_executable
    win32api.UpdateResource(hdst, pefile.RESOURCE_TYPE['RT_VERSION'], 1, info.toRaw())
                                                                         ^^^^^^^^^^^^
  File "D:\env\fbt\Lib\site-packages\PyInstaller\utils\win32\versioninfo.py", line 122, in toRaw
    rawffi = self.ffi.toRaw()
             ^^^^^^^^^^^^^^^^
  File "D:\env\fbt\Lib\site-packages\PyInstaller\utils\win32\versioninfo.py", line 247, in toRaw
    return struct.pack(
           ^^^^^^^^^^^^
struct.error: argument out of range

我的 file_version_info.txt 是如下方式产生的。

import pyinstaller_versionfile

pyinstaller_versionfile.create_versionfile(
    output_file="file_version_info.txt",
    version="1.2.20230913.4",
    company_name="My Imaginary Company",
    file_description="Simple App",
    internal_name="Simple App",
    legal_copyright="© My Imaginary Company. All rights reserved.",
    original_filename="SimpleApp.exe",
    product_name="Simple App"
)

https://stackoverflow.com/questions/14624245/what-does-a-version-file-look-like

错误是在版本号这里:

版本号的每一部分(主版本, 次版本, 构建号, 修订号) 都应该是一个16位的数字,这意味着每部分的范围是0到65535。而上面的版本号中,20230913 超出了这个范围。