python - 导入错误: cannot import name 'LinearNDInterpolator'

标签 python scipy cx-freeze

我已经用 Python 3.6.2 编写了一个程序,我试图使用 cx_Freeze 卡住该程序以进行分发。但是当我尝试运行生成的可执行文件时,我遇到了一个奇怪的错误(我的基本程序使用 pyLDAvis.sklearn)。错误重现如下:

Traceback (most recent call last):

File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "MYPROGRAM.py", line 1474, in <module>
    import pyLDAvis.sklearn
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pyLDAvis\__init__.py", line 44, in <module>
    from ._display import *
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pyLDAvis\_display.py", line 13, in <module>
    from ._prepare import PreparedData
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\pyLDAvis\_prepare.py", line 15, in <module>
    from scipy.stats import entropy
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\stats\__init__.py", line 345, in <module>
    from .stats import *
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\stats\stats.py", line 171, in <module>
    from . import distributions
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\stats\distributions.py", line 10, in <module>
    from ._distn_infrastructure import (entropy, rv_discrete, rv_continuous,
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 16, in <module>
    from scipy.misc import doccer
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\misc\__init__.py", line 68, in <module>
    from scipy.interpolate._pade import pade as _pade
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\interpolate\__init__.py", line 187, in <module>
    from .ndgriddata import *
  File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\scipy\interpolate\ndgriddata.py", line 11, in <module>
    from .interpnd import LinearNDInterpolator, NDInterpolatorBase, \
ImportError: cannot import name 'LinearNDInterpolator'

我的 cx_freeze 脚本的可执行选项如下:

build_exe_options = {
                        "packages": ["os","textwrap","msvcrt","warnings","time","datetime","platform","sklearn","operator","nltk.tokenize","stop_words","pandas","nltk.stem.porter","sklearn.feature_extraction.text","sklearn.decomposition","progressbar","numpy","packaging","asyncio",
                        ], 

                        "includes": ["appdirs","packaging.version","packaging.specifiers","packaging.requirements","pyLDAvis.sklearn","pyLDAvis.urls","scipy.sparse.csgraph._validation"],

                        "excludes" : ["tkinter","sqlite3"],

                        "include_msvcr" : True
                    }

我似乎找不到 scipy.interpolate 或 scipy.interpolate.interpnd 的任何组合来放入让其工作的构建选项 - 我始终收到“无法导入名称'LinearNDInterpolator'”错误。重新安装 scipy 没有帮助。

有人可以建议吗?难道我注定无法卡住我的代码吗?

最佳答案

  1. 您是否尝试过添加

    import scipy.interpolate.interpnd
    

    在你的基本程序中(而不是在安装脚本中)?然后会发生什么?

  2. 根据我目前的经验,需要将scipy添加到packages列表中,才能让正确包含它>cx_Freeze。但随后还需要将 scipy.spatial.cKDTree 添加到排除列表中,因为 this issue 。因此,请尝试在设置脚本中使用以下选项:

    build_exe_options = {
                    "packages": ["os","textwrap","msvcrt","warnings","time","datetime","platform","sklearn","operator","nltk.tokenize","stop_words","pandas","nltk.stem.porter","sklearn.feature_extraction.text","sklearn.decomposition","progressbar","numpy","packaging","asyncio","scipy"
                    ], 
    
                    "includes": ["appdirs","packaging.version","packaging.specifiers","packaging.requirements","pyLDAvis.sklearn","pyLDAvis.urls","scipy.sparse.csgraph._validation"],
    
                    "excludes" : ["tkinter","sqlite3","scipy.spatial.cKDTree"],
    
                    "include_msvcr" : True
                }
    
  3. 补充说明:build_exe 选项 "include_msvcr": True 似乎对 cx_Freeze 版本 5.0.2 没有影响, 5.1.1 和 6.0b1。请参阅this issue ,我在那里的帖子 (jpeg13) 包含更多详细信息。您可能需要使用 build_exe 选项 include_files 手动添加 MSVCR DLL。

关于python - 导入错误: cannot import name 'LinearNDInterpolator' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000952/

相关文章:

python - cx_freeze python sqlite3 数据库在 build.exe 后不起作用?

python - 如何使用 beautifulsoup 提取内容

python - 有人可以解释一下 np.log 是做什么的吗?

python-3.x - 将 "matplotlib"添加到 cx_Freeze 中的包不起作用

python - 在 PyQt 应用程序上使用 cx_freeze 时出现语法错误

python - 有效地将稀疏 (csc) 矩阵列添加到形状正确的密集 numpy 数组中

python - 使用回溯的唯一排列

python - Python 中的 Matlab lsqnonlin

python - 如何将复杂包的 python 类型提示指定为 opencv 或 tensorflow?

python - 使用来自 SciPy 的 wavfile.write 在 Python 中编写 wav 文件