Python Sklearn / Scikit和cx_freeze:模块“ sklearn.tree._criterion”没有属性“ Criterion”

时间:2018-10-17 08:10:31

标签: python module scikit-learn cx-freeze imblearn

我目前正在尝试将使用 Sklearn模块的Python应用程序放在独立的.exe文件中。

我当前的cx_freeze setup.py看起来像这样:

import os
from cx_Freeze import setup, Executable

base = "Win32GUI"

os.environ['TCL_LIBRARY'] = r'C:\Users\...\AppData\Local\Continuum\anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'C:\Users\...\AppData\Local\Continuum\anaconda3\tcl\tk8.6'

executables = [Executable("gui_handler.py", base=base)]

packages = ["idna", "numpy", "sklearn", "nltk", "stop_words", "scipy", "scipy.sparse.csgraph._validation",
            "scipy.spatial.ckdtree", "PyQt5", "imblearn.under_sampling", "imblearn.over_sampling",
            "sklearn.tree._criterion"]

options = {
    'build_exe': {
        "packages": packages,
        "excludes": ["scipy.spatial.cKDTree"],
        "zip_include_packages": ["sklearn", "nltk", "idna"] # PyQt5 zippen verschlechtert die Optik
    },
}

setup(
    name="<any name>",
    options=options,
    version="1.0",
    description='<any description>',
    executables=executables
)

以前运行良好,但我的应用程序的最新版本使用模块imblearn 。我认为这会导致此问题(双击.exe文件几秒钟后出现):

Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "gui_handler.py", line 8, in <module>
  File "C:\Users\...\Documents\PycharmProjects\MachineLearning\build\exe.win-amd64-3.6\TextClassification\init_predict.py", line 16, in <module>
    from imblearn.under_sampling import RandomUnderSampler
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\imblearn\under_sampling\__init__.py", line 6, in <module>
    from .prototype_generation import ClusterCentroids
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\imblearn\under_sampling\prototype_generation\__init__.py", line 6, in <module>
    from .cluster_centroids import ClusterCentroids
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\imblearn\under_sampling\prototype_generation\cluster_centroids.py", line 14, in <module>
    from sklearn.cluster import KMeans
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\cluster\__init__.py", line 6, in <module>
    from .spectral import spectral_clustering, SpectralClustering
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\cluster\spectral.py", line 17, in <module>
    from ..manifold import spectral_embedding
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\manifold\__init__.py", line 9, in <module>
    from .t_sne import TSNE
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\manifold\t_sne.py", line 25, in <module>
    from . import _barnes_hut_tsne
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "ExtensionLoader_sklearn_manifold__barnes_hut_tsne.py", line 23, in <module>
  File "ExtensionLoader_sklearn_manifold__barnes_hut_tsne.py", line 15, in __bootstrap__
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
  File "sklearn\neighbors\quad_tree.pxd", line 54, in init sklearn.manifold._barnes_hut_tsne
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "ExtensionLoader_sklearn_neighbors_quad_tree.py", line 23, in <module>
  File "ExtensionLoader_sklearn_neighbors_quad_tree.py", line 15, in __bootstrap__
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
  File "sklearn\tree\_criterion.pxd", line 21, in init sklearn.neighbors.quad_tree
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\tree\__init__.py", line 6, in <module>
    from .tree import DecisionTreeClassifier
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\tree\tree.py", line 40, in <module>
    from ._criterion import Criterion
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "ExtensionLoader_sklearn_tree__criterion.py", line 23, in <module>
  File "ExtensionLoader_sklearn_tree__criterion.py", line 15, in __bootstrap__
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
  File "sklearn\tree\_splitter.pxd", line 34, in init sklearn.tree._criterion
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "ExtensionLoader_sklearn_tree__splitter.py", line 23, in <module>
  File "ExtensionLoader_sklearn_tree__splitter.py", line 15, in __bootstrap__
  File "C:\Users\...\AppData\Local\Continuum\anaconda3\lib\imp.py", line 343, in load_dynamic
    return _load(spec)
  File "sklearn\tree\_criterion.pxd", line 21, in init sklearn.tree._splitter
AttributeError: module 'sklearn.tree._criterion' has no attribute 'Criterion'

我查看了sklearn\tree\tree.py文件,确实有这个import语句:

from ._criterion import Criterion

同一文件夹中包含以下文件:

_criterion.cp36-win_amd64.pyd _splitter.cp36-win_amd64.pyd _tree.cp36-win_amd64.pyd _utils.cp36-win_amd64.pyd

据我所知,它们是python模块文件。

如果我从 PyCharm IDE 执行该应用程序,则该应用程序将运行良好,因此我可以假设我的程序甚至不需要 Criterion模块(和{{ 1}}只是加载它,因为它加载了所有内容),因此在运行时我没有收到错误消息。或者实际上是缺少该模块。

有什么办法可以解决此问题?

1 个答案:

答案 0 :(得分:0)

好的,我自己解决了这个问题。

我刚刚删除了行

zip_include_packages": ["sklearn", "nltk", "idna"]

来自我的cx_freeze_setup.py