Search code examples
pythonmatplotlibcx-freeze

cx_Freeze TypeError dist must be a Distribution instance


I am not finding a specific topic on this issue with the setup file for cx_Freeze. I am trying to create an exe for my program but something is not wright with the distutils. I am not able to locate an update whl for this library so I am not sure if there is a known fix for this.

The program works fine without error.

Does anyone know why this issue exist. Please note I am not able to use pip from inside my work network so I have to do everything with whl, tar.gz' and egg files to install libraries. This is why I am trying to find an updated whl file for distutils.

My setup.py file.

from cx_Freeze import setup, Executable

base = None    

build_exe_options = {'packages': ['idna',
                                  'json',
                                  'tkinter',
                                  'operator',
                                  'clipboard',
                                  'matplotlib',
                                  'tkinter.ttk ',
                                  'matplotlib.pyplot',
                                  'matplotlib.backends.backend_tkagg'],
                     'include_files': ['tracker1.json', 'tracker2.json']}

setup(
    name='<NAME>',
    options={'build.exe': build_exe_options},
    version='<0.2>',
    description='<some random desc>',
    executables=[Executable('MAIN.py', base=base)]
)

The error:

"C:\Users\user_name\Desktop\Python 3.6.2\python.exe" "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pycharm\pycharm_setup_runner.py" "C:\Users\user_name\Desktop\Python Work Projects\GATE\setup.py"
Testing started at 2:55 PM ...
Traceback (most recent call last):
running pycharm_test
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.2.3\helpers\pycharm\pycharm_setup_runner.py", line 26, in <module>
    exec (fh.read(), globals(), locals())
  File "<string>", line 21, in <module>
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\cx_Freeze\dist.py", line 349, in setup
    distutils.core.setup(**attrs)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\core.py", line 148, in setup
    dist.run_commands()
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 972, in run_command
    cmd_obj = self.get_command_obj(command)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\dist.py", line 847, in get_command_obj
    cmd_obj = self.command_obj[command] = klass(self)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\site-packages\setuptools\__init__.py", line 147, in __init__
    _Command.__init__(self, dist)
  File "C:\Users\user_name\Desktop\Python 3.6.2\lib\distutils\cmd.py", line 57, in __init__
    raise TypeError("dist must be a Distribution instance")
TypeError: dist must be a Distribution instance

Solution

  • After a lot of digging and dealing with several errors after I got the file to compile to an exe I fixed my issue.

    The larger portion of the problem was related to the setup.py. I had to add a few things to make it all compile properly.

    new setup.py file:

    from cx_Freeze import setup, Executable
    import os
    base = "Win32GUI" # this lets the exe run without the console popping up.
    
    # I had to add these 2 in order for tkinter to compile properly
    os.environ['TCL_LIBRARY'] = r'C:\Users\user_name\Desktop\Python3.6.2\tcl\tcl8.6'
    os.environ['TK_LIBRARY'] = r'C:\Users\user_name\Desktop\Python3.6.2\tcl\tk8.6'
    
    # eventhough numpy is not part of my main imports in my MAIN file I still needed to 
    # provide 'numpy.core._methods' and 'numpy.lib.format' in the packages list for 
    # my plot to work. I am assuming it is because `matplotlib` is using `numpy` somewhere.
    build_exe_options = {'packages': ['numpy.core._methods',
                                      'numpy.lib.format',
                                      'idna',
                                      'json',
                                      'tkinter',
                                      'operator',
                                      'clipboard',
                                      'matplotlib',
                                      'tkinter.ttk',
                                      'matplotlib.pyplot',
                                      'matplotlib.backends.backend_tkagg'],
                         'include_files': [r'tracker1.json',
                                           r'tracker2.json',
                                           "tcl86t.dll",
                                           "tk86t.dll"]}
    # On jpeg's advice I changed build.exe to build_exe though I am not sure what the change was for.
    setup(
        name='<CCC>',
        options={'build_exe': build_exe_options},
        version='<0.2>',
        description='<CCC - Copy Count Chart!.>',
        executables=[Executable(r'C:\path\MAIN.py', base=base)]
    )
    

    After that I had to run the build command in the CMD or I would end up getting errors in my IDE console.

    I am not sure why but it seams it is required to use the Command Prompt to rune the setup.py file or else it just wont work.

    Here is the command if anyone else needs it:

    python setup.py build
    

    Keep in mind you may need to use a complete file path to work with the setup file. I had to the way my working directory was set up by using the below command:

    python "C:\Users\user_name\Desktop\Python Work Projects\PROJECT\setup.py" build