ImportError: cannot import name '_ni_support' — Using cx-freeze with scipy

爱⌒轻易说出口 提交于 2021-02-10 14:14:13

问题


I have used cx-freeze to make an executable file from my python 3 script. The issue is that apparently cx-freeze is having a hard time importing scipy scripts. I had to resolve other issues previously, e.g. adding the tcl library files manually. Anyway, my setup files is below:

from cx_Freeze import setup, Executable
import os 

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

additional_mods = ['numpy.core._methods', 'numpy.lib.format']

setup(name = "Curve Characterization" ,
  options = {'build_exe': {'includes': additional_mods}},
  version = "0.1" ,
  description = "" ,
  executables = [Executable("curvCharLite.py")])

This is the error that I get:

ImportError: cannot import name '_ni_support'

Full details

Traceback (most recent call last):
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
 File "curvCharLite.py", line 21, in <module>
 File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\__init__.py", line 8, in <module>
from .detection import (Binseg, BottomUp, Dynp, Omp, OmpK, Pelt, Window,
 File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\detection\__init__.py", line 51, in <module>
from .window import Window
 File "c:\users\Gobryas\documents\mo\project 1\ruptures\ruptures\detection\window.py", line 112, in <module>
from scipy.signal import argrelmax
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\signal\__init__.py", line 311, in <module>
from ._savitzky_golay import savgol_coeffs, savgol_filter
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\signal\_savitzky_golay.py", line 6, in <module>
from scipy.ndimage import convolve1d
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\ndimage\__init__.py", line 161, in <module>
from .filters import *
 File "C:\Users\Gobryas\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\ndimage\filters.py", line 35, in <module>
from . import _ni_support
ImportError: cannot import name '_ni_support'

PS 1: There is a similar issue asked here, but no helpful answer really.

PS 2: I'm using scipy 1.1.0, cx-freeze 5.1.1, and python 3.6.5


回答1:


So, I think I found a solution for this problem inspired by the post by @fepzzz. Looks like there are bad conflicts between cx-freeze and scipy which can be avoided as follows. I needed to modify the include_files build option as below:

import os
import scipy

includefiles_list=[]
scipy_path = os.path.dirname(scipy.__file__)
includefiles_list.append(scipy_path)

build_options = dict(packages=['matplotlib'], #this line solves an issue w/ matplotlib
                 include_files=includefiles_list, #this line is for scipy issue
                 includes=['matplotlib.backends.backend_qt5agg']) #this line solves another issue w/ matplotlib

Hope this helps others.



来源:https://stackoverflow.com/questions/50807021/importerror-cannot-import-name-ni-support-using-cx-freeze-with-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!