cx_Freeze TypeError dist必须是分发实例

时间:2019-02-18 21:17:23

标签: python matplotlib cx-freeze

我在cx_Freeze的安装文件中找不到关于此问题的特定主题。 我正在尝试为我的程序创建一个exe,但是distutils不能正常运行。我找不到该库的更新whl,所以不确定是否有已知的修复程序。

程序正常运行,没有错误。

有人知道为什么存在此问题。 请注意,我无法在工作网络中使用pip,因此我必须对whl,tar.gz'和egg文件进行所有操作以安装库。 这就是为什么我要为whl找到更新的distutils文件的原因。

我的setup.py文件。

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)]
)

错误:

"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

3 个答案:

答案 0 :(得分:0)

尝试使用以下方式更新setuptoolsGohlke's Windows binaries中的setuptools‑40.8.0‑py2.py3‑none‑any.whl,另请参见TypeError: dist must be a Distribution instance

答案 1 :(得分:0)

在我将文件编译成exe后,经过大量挖掘并处理了一些错误,我解决了问题。

问题的大部分与setup.py有关。我必须添加一些东西才能使其全部正确编译。

新的setup.py文件:

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)]
)

之后,我必须在CMD中运行build命令,否则最终会在IDE控制台中出现错误。

我不确定为什么,但是它接缝了需要使用命令提示符来运行setup.py文件,否则它将无法正常工作。

如果其他人需要它,这是命令:

python setup.py build

请记住,您可能需要使用完整的文件路径来使用安装文件。我必须使用以下命令来设置工作目录:

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

答案 2 :(得分:0)

我有一个类似的问题,可以通过在顶部添加import setuptools来解决。我认为它可以更正cx_Freeze中的某些导入,但是我不确定是否相关。

import setuptools
from cx_Freeze import setup, Executable

...

这也无需添加'numpy.core._methods', 'numpy.lib.format'