尝试将Python脚本编译为exe时出错 [英] Error while trying to compile Python script to exe

查看:73
本文介绍了尝试将Python脚本编译为exe时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  print("hello") 

我的 setup.py :

来自distutils.core导入设置的

 导入py2exe,sys,ossys.argv.append('py2exe')设置(名称=你好",description ='hello script',版本='1.0',options = {'py2exe':{'bundle_files':1,'compressed':True,'dist_dir':'.','dll_excludes':['w9xpopen.exe']}},console = [{'script':r"hello.py"}],zipfile =无,) 

我跑步:

  py -3 setup.py安装 

错误:

  py -3 setup.py安装运行安装运行构建运行install_egg_info删除C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ Lib \ site-packages \ hello-1.0-py3.7.egg-info编写C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ Lib \ site-packages \ hello-1.0-py3.7.egg-info运行py2exe追溯(最近一次通话):在< module>中的文件"setup.py"第19行.zipfile =无,设置中的文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ distutils \ core.py",第148行dist.run_commands()文件``C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ distutils \ dist.py''行966,在run_commands中self.run_command(cmd)在run_command中的文件``C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ distutils \ dist.py''行985cmd_obj.run()运行中的文件``C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ distutils_buildexe.py'',行188self._run()_run中的文件``C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ distutils_buildexe.py'',第267行builder.analyze()分析中的文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ runtime.py",行160self.mf.import_hook(modname)在import_hook的第120行中,文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ mf3.py"模块= self._gcd_import(名称)_gcd_import中的文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ mf3.py",行274返回self._find_and_load(name)在_find_and_load的第357行的文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ mf3.py"self._scan_code(module .__ code__,module)文件``C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ mf3.py'',行388,用_scan_code对于什么,self._scan_opcodes(code)中的参数:文件"C:\ Users \ alonat \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ py2exe \ mf3.py",第417行,以_scan_opcodes产生存储",(名称[oparg],)IndexError:元组索引超出范围 

您知道如何解决此错误吗?

解决方案

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

但是,还有其他库(例如Pyinstaller)运行良好,并且与各种Python版本(从2.7到3.5+)兼容.

检查一下,实际上非常容易:)

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

I'm trying to compile python script to exe. My script - hello.py:

print("hello")

My 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,
)

I run:

py -3 setup.py install

The error:

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

Do you know how to resolve this error?

解决方案

py2exe seems to support up to Python 3.4 (thanks Michael Butscher)

However, there are other libraries such as Pyinstaller which work just fine, and are compatible with a variety of Python versions (from Python 2.7 to 3.5+)

Check it out, it's actually really easy :)

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

这篇关于尝试将Python脚本编译为exe时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆