简体   繁体   中英

ImportError: cannot import name 'LinearNDInterpolator'

I've written a program in Python 3.6.2 that I'm trying to freeze for distribution using cx_Freeze. But I'm getting a strange error when trying to run the resulting executable (my base program uses pyLDAvis.sklearn). The error is reproduced below:

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'

The executable options for my cx_freeze script are below:

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
                    }

I can't seem to find any combination of scipy.interpolate or scipy.interpolate.interpnd to put in the build options that lets this work -- I consistently get the "can't import name 'LinearNDInterpolator'" error. Reinstalling scipy doesn't help.

Can anyone advise? Am I just doomed to not be able to freeze my code?

  1. Have you tried to add

     import scipy.interpolate.interpnd 

    in your base program (not in the setup script)? What happens then?

  2. According to the experiences I have made so far, one needs to add scipy to the packages list in order to let it be included correctly by cx_Freeze . But then one also needs to add scipy.spatial.cKDTree to the excludes list due to this issue . Try thus to use the following options in your setup script:

     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. Additional remark: the build_exe option "include_msvcr": True seems to have no effect with cx_Freeze versions 5.0.2, 5.1.1, and 6.0b1. See this issue , my post there (jpeg13) contains some more details. You might need to add the MSVCR DLLs manually using the build_exe option include_files .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM