cx_Freeze TypeError dist 必须是 Distribution 实例

cx_Freeze TypeError dist must be a Distribution instance

我没有在 cx_Freeze 的安装文件中找到关于此问题的特定主题。 我正在尝试为我的程序创建一个 exe,但是 distutils 有一些不对劲。我无法找到此库的更新 whl,因此我不确定是否有已知的修复方法。

程序运行良好,没有错误。

有谁知道为什么会出现这个问题。 请注意,我无法从工作网络内部使用 pip,因此我必须使用 whl、tar.gz' 和 egg 文件来安装库。 这就是为什么我要为 distutils.

寻找更新的 whl 文件

我的 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

尝试使用例如更新 setuptools setuptools‑40.8.0‑py2.py3‑none‑any.whl 来自 Gohlke's Windows binaries See also TypeError: dist must be a Distribution instance.

在我将文件编译成 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 中执行构建命令,否则我最终会在 IDE 控制台中收到错误消息。

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

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

python setup.py build

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

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

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

import setuptools
from cx_Freeze import setup, Executable

...

这在不添加 'numpy.core._methods', 'numpy.lib.format' 的情况下也有效。