IndexError: tuple index out of range in Python

IndexError: tuple index out of range in Python is a Python error which occurs when py2exe is not compatible with your pyton version.

This post is a guide showing you why you are having this error and how you can get rid of it in the fastest and easiest way possible, I will also present to you other alternative solutions that could help.

Explaining the Error :

First, we need to reproduce the error, let us start by checking out the message of the error.

                                                                       #
running py2exe
Traceback (most recent call last):
  File "main.py", line 4, in <module>
    setup(console=['Storybookmaker.py'])
    dist.run_commands()
.
.
.
    for what, args in self._scan_opcodes(code):
    yield "store", (names[oparg],)
IndexError: tuple index out of range
                                                                       #

Main.py is an example of a file, that could throw the error.

                                                                       #
from distutils.core import main
import py2exe
main(console=['Storybookmaker.py'])
                                                                       #

Now that we have reproduced the error.

Bellow I will make my best attempt at solving the error and present multiple possible solutions.

Solution 1 : use pyinstaller

The solution is simple, but before we talk about solutions, we should understand that the error happens when py2exe does not support the version of python you have.

The first solution is to use pyinstaller instead of py2exe 

First, install pyinstaller using the command bellow

                                                                       #
pip install pyinstaller
                                                                       #

Then run the following command to create a folder called myprogram which resides inside a directory named dist, the myprogram folder will contain myprogram.exe

                                                                       #
pyinstaller myprogram.py
                                                                       #

For more advanced pyinstaller stuff you should read the pyinstaller manual. https://pyinstaller.org/en/stable/

I hope this solved the issue. if not, continue reading.

Solution 2 : downgrade your python version

Another solution is to downgrade your python version, if you do not want to do that. Jump to the last solution.

First, start by installing Python 3.4.3.

Then install py2exe  like you did before (for you current version of python).

Solution 3 : use cx_freeze

Do not use py2exe and use cx_freeze

First,  you should make sure to get the version of cx_freeze that is compatible with your python version. 

Then you should crate a setup file, luckily one of the developers who had the same issue shared the code of the setup file that fixed his issue.

First, create a file called setup.py, in the beggining start with the imports

                                                                       #
import sys, os
from cx_Freeze import setup, Executable
__version__ = "1.1.0"
                                                                       #

Followed by file, includes and packages

                                                                       #
include_files = ['logging.ini', 'config.ini', 'running.png']
excludes = ["tkinter"]
packages = ["os", "idna", "requests","json","base64","pyodbc"]
                                                                       #

Then the setup

                                                                       #
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 run the command bellow

                                                                       #
python setup.py bdist_msi
                                                                       #

I hope the commands above fixed your problem, good luck with the scripts to come.

Summing-up

I hope this article helped you solve the error IndexError: tuple index out of range in Python, If not, I hope the solutions presented here guided you at least in the right direction.

Keep coding and cheers, see you in another article. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/