Python IndexError:使用py2exe时元组索引超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41578808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IndexError: tuple index out of range when using py2exe
提问by Dennis
I'm currently trying to make an executable using py2exe. I use Python 3.6. The script I'm using imports openpyxl
and pptx
and runs fine when I use Pycharm or run the script using the command window.
我目前正在尝试使用 py2exe 制作可执行文件。我使用 Python 3.6。当我使用 Pycharm 或使用命令窗口运行脚本时,我使用的脚本导入openpyxl
和pptx
运行良好。
The output produces the error:
输出产生错误:
IndexError: tuple index out of range
Below you can find the cmd
output:
您可以在下面找到cmd
输出:
C:\Python36>python setup.py py2exe
running py2exe
Traceback (most recent call last):
File "setup.py", line 4, in <module>
setup(console=['Storybookmaker.py'])
File "C:\Python36\lib\distutils\core.py", line 148, in setup
dist.run_commands()
File "C:\Python36\lib\distutils\dist.py", line 955, in run_commands
self.run_command(cmd)
File "C:\Python36\lib\distutils\dist.py", line 974, in run_command
cmd_obj.run()
File "C:\Python36\lib\site-packages\py2exe\distutils_buildexe.py", line 188, in run
self._run()
File "C:\Python36\lib\site-packages\py2exe\distutils_buildexe.py", line 267, in _run
builder.analyze()
File "C:\Python36\lib\site-packages\py2exe\runtime.py", line 160, in analyze
self.mf.import_hook(modname)
File "C:\Python36\lib\site-packages\py2exe\mf3.py", line 120, in import_hook
module = self._gcd_import(name)
File "C:\Python36\lib\site-packages\py2exe\mf3.py", line 274, in _gcd_import
return self._find_and_load(name)
File "C:\Python36\lib\site-packages\py2exe\mf3.py", line 357, in _find_and_load
self._scan_code(module.__code__, module)
File "C:\Python36\lib\site-packages\py2exe\mf3.py", line 388, in _scan_code
for what, args in self._scan_opcodes(code):
File "C:\Python36\lib\site-packages\py2exe\mf3.py", line 417, in _scan_opcodes
yield "store", (names[oparg],)
IndexError: tuple index out of range
C:\Python36>
What causes the IndexError
?
是什么原因造成的IndexError
?
Edit: here is the setup.py
file:
编辑:这里是setup.py
文件:
from distutils.core import setup
import py2exe
setup(console=['Storybookmaker.py'])
回答by ShadowRanger
Python 3.6 completely redesigned the bytecode for CPython(it's not a "byte" code at all anymore, it's a wordcode, where all opcodes are two bytes wide instead of 1-3).
Python 3.6完全重新设计了 CPython 的字节码(它根本不再是“字节”代码,而是字码,其中所有操作码的宽度都是两个字节而不是 1-3 个字节)。
The failure you're seeing occurs in py2exe
opcode parsing code, which, given the most recent posted version of py2exe
only claims support for 3.3 and 3.4, could not possibly have knowledge of, or support for, the new wordcode opcodes; they hadn't even been conceived of at the time py2exe
was last updated. The bytecode often changes in small ways from version to version that could break even Python 3.5 (given only 3.3 and 3.4 support is claimed explicitly), but 3.6 is 100% guaranteed to fail.
您看到的失败发生在py2exe
操作码解析代码中,鉴于最近发布的py2exe
仅声称支持 3.3 和 3.4 的版本,该代码不可能了解或支持新的字码操作码;在py2exe
上次更新时,它们甚至还没有被构思出来。字节码在不同版本之间经常发生微小的变化,甚至可能破坏 Python 3.5(鉴于仅明确声明支持 3.3 和 3.4),但 3.6 保证 100% 失败。
Update:At this point (November 2019), it's been over five years since the last py2exe
release, and by the beginning of 2020 (when Python 2 support lapses completely), it will not run on any supported version of Python (3.4 is already out of support). I think it's safe to say the project is abandoned; find other options, e.g. cx_Freeze
or PyInstaller
.
更新:此时(2019 年 11 月),距离上次py2exe
发布已经过去五年多了,到 2020 年初(当 Python 2 支持完全失效时),它将无法在任何受支持的 Python 版本上运行(3.4 已经发布)支持)。我认为可以肯定地说该项目已被放弃;找到其他选项,例如cx_Freeze
或PyInstaller
。
回答by recurseuntilfor
The solution I used was to use PyInstaller as an alternative because Py2Exe stopped development at python 3.4 and will not work with newer versions.
我使用的解决方案是使用 PyInstaller 作为替代方案,因为 Py2Exe 在 python 3.4 上停止了开发并且不适用于较新的版本。
C:/>pip install pyinstaller
C:/>pyinstaller yourprogram.py
This will create a subdirectory called dist with the yourprogram.exe contained in a folder called yourprogram.
这将创建一个名为 dist 的子目录,其中 yourprogram.exe 包含在名为 yourprogram 的文件夹中。
Use -F to place all generated files in one executable file.
使用 -F 将所有生成的文件放在一个可执行文件中。
C:/>pyinstaller -F yourprogram
Use can use -w to if you want to remove console display for GUI's.
如果要删除 GUI 的控制台显示,可以使用 -w 到。
C:/>pyinstaller -w yourprogram.py
Putting it all togerther.
把这一切放在一起。
C:/>pyinstaller -w -F yourprogram.py
Read more about PyInstaller here.
在此处阅读有关 PyInstaller 的更多信息。
Python version 3.7.3.
Python 版本 3.7.3。
回答by Ezequiel Alanís
I had same problem, as workaround I used cx_freeze. My app is based on wxPython, windows 10, python 3.6, cx_freeze 5.5.1
我遇到了同样的问题,作为解决方法,我使用了 cx_freeze。我的应用程序基于 wxPython、windows 10、python 3.6、cx_freeze 5.5.1
This is the setup file that I used and I got msi file on dist folder.
这是我使用的安装文件,我在 dist 文件夹中获得了 msi 文件。
#setup.py
import sys, os
from cx_Freeze import setup, Executable
__version__ = "1.1.0"
include_files = ['logging.ini', 'config.ini', 'running.png']
excludes = ["tkinter"]
packages = ["os", "idna", "requests","json","base64","pyodbc"]
setup(
name = "appname",
description='App Description',
version=__version__,
options = {"build_exe": {
'packages': packages,
'include_files': include_files,
'excludes': excludes,
'include_msvcr': True,
}},
executables = [Executable("b2b_conn.py",base="Win32GUI")]
)`
then python setup.py bdist_msi
然后 python setup.py bdist_msi
回答by Ricardo Martínez
I tried a workaround, by installing Python 3.4.3:
我尝试了一种解决方法,通过安装 Python 3.4.3:
C:\socket> c:\Python34\python.exe setup.py py2exe
1) enter in your script folder
1)进入你的脚本文件夹
2) deactivate any antivirus that you have (weird thing, know by another SO question xD)
2)停用您拥有的任何防病毒软件(奇怪的是,另一个SO问题xD知道)
3) call the python 3.4.3 interpreter by his absolute path, in my case, i've installed in:
3)通过他的绝对路径调用python 3.4.3解释器,在我的例子中,我已经安装了:
C:\Python34
4) execute the command
4)执行命令
C:\Python34\python.exe setup.py py2exe
回答by Rasjid Wilcox
I've had success with a Python 3.6 program using the fork of py2exe at https://github.com/albertosottile/py2exe.
我在https://github.com/albertosottile/py2exe 上使用 py2exe 的分支在 Python 3.6 程序上取得了成功。