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. The output produces the error:
IndexError: tuple index out of range
Below you can find the cmd output:
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?
Edit: here is the setup.py
file
from distutils.core import setup
import py2exe
setup(console=['Storybookmaker.py'])
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).
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.
The solution I used as Py2Exe stopped development at python 3.4 and will not work with newer versions was to use PyInstaller.
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.
Use -F to place all generated files in one executable file.
C:/>pyinstaller -F yourprogram
Use can use -w to if you want to remove console display for GUI's.
C:/>pyinstaller -w yourprogram.py
Putting it all togerther.
C:/>pyinstaller -w -F yourprogram.py
Read more about PyInstaller here.
Python version 3.7.3.
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
This is the setup file that I used and I got msi file on dist folder.
#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
Dennis, a few hours later than you, i test the same thing without luck, i have installed Python 3.6 and by the moment, it doesn't work.
I try a walkaround, i've installed Python 3.4.3, and try this:
C:\socket> c:\Python34\python.exe setup.py py2exe
1) enter in your script folder 2) deactivate any antivirus that you have (weird thing, know by another SO question xD)
2) call the python 3.4.3 interpreter by his absolute path, in my case, i've installed in:
C:\Python34
3) execute the command
c:\Python34\python.exe setup.py py2exe
hope that help like me
来源:https://stackoverflow.com/questions/41578808/python-indexerror-tuple-index-out-of-range-when-using-py2exe