尝试将 Python 脚本编译为 exe 时出错

Error while trying to compile Python script to exe

我正在尝试将 python 脚本编译为 exe。 我的脚本 - hello.py:

print("hello")

我的setup.py:

from distutils.core import setup
import py2exe, sys, os


sys.argv.append('py2exe')

setup(
    name = 'hello',
    description = 'hello script',
    version = '1.0',
    
    options = {'py2exe': {'bundle_files': 1, 'compressed': True,'dist_dir': ".",'dll_excludes':['w9xpopen.exe']}},
    console = [{'script': r"hello.py"}],
    zipfile = None,
)

我运行:

py -3 setup.py install

错误:

py -3 setup.py install
running install
running build
running install_egg_info
Removing C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\hello-1.0-py3.7.egg-info
Writing C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\hello-1.0-py3.7.egg-info
running py2exe
Traceback (most recent call last):
  File "setup.py", line 19, in <module>
    zipfile = None,
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\dist.py", line 966, in run_commands
    self.run_command(cmd)
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\distutils\dist.py", line 985, in run_command
    cmd_obj.run()
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
    self._run()
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
    builder.analyze()
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\runtime.py", line 160, in analyze
    self.mf.import_hook(modname)
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
    module = self._gcd_import(name)
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
    return self._find_and_load(name)
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 357, in _find_and_load
    self._scan_code(module.__code__, module)
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 388, in _scan_code
    for what, args in self._scan_opcodes(code):
  File "C:\Users\alonat\AppData\Local\Programs\Python\Python37-32\lib\site-packages\py2exe\mf3.py", line 417, in _scan_opcodes
    yield "store", (names[oparg],)
IndexError: tuple index out of range

你知道如何解决这个错误吗?

py2exe 似乎最多支持 Python 3.4(感谢 Michael Butscher)

但是,还有其他库(例如 Pyinstaller)可以正常工作,并且兼容各种 Python 版本(从 Python 2.7 到 3.5+)

看看吧,其实很简单:)

https://pyinstaller.readthedocs.io/en/stable/