Scipy和CX_freeze - 导入Scipy出错:在Scipy源目录中无法导入Scipy。

7
我在使用cx_freeze和scipy编译exe时遇到了问题。特别是,我的脚本使用了:
from scipy.interpolate import griddata

构建过程似乎已经成功完成,但是当我尝试运行编译后的exe文件时,会出现以下错误信息。
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 27, in <module>
    exec(code, m.__dict__)
  File "gis_helper.py", line 13, in <module>
  File "C:\Python27\lib\site-packages\scipy\__init__.py", line 103, in <module>
    raise ImportError(msg)
ImportError: Error importing scipy: you cannot import scipy while
        being in scipy source directory; please exit the scipy source
        tree first, and relaunch your python intepreter.

查看scipy\_init\_\_.py文件后,可以看到以下内容:

if __SCIPY_SETUP__:
    import sys as _sys
    _sys.stderr.write('Running from scipy source directory.\n')
    del _sys
else:
    try:
        from scipy.__config__ import show as show_config
    except ImportError:
        msg = """Error importing scipy: you cannot import scipy while
        being in scipy source directory; please exit the scipy source
        tree first, and relaunch your python intepreter."""
        raise ImportError(msg)

我不确定问题出在哪里,但是看起来错误是因为scipy配置文件有问题。可能没有被包含在构建过程中。我是一个新手,希望有更多经验的使用cxfreeze生成构建的人可以解决这个问题。

顺便说一下,使用的scipy是从这里下载的二进制文件安装的。

2个回答

13

我也遇到了同样的问题。我在由cx_freeze生成的setup.py中添加了以下代码:

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

然后,将includefiles_list作为build_exe参数的一部分使用:

build_options = dict(packages=[], include_files=includefiles_list)

setup(name="foo", options=dict(build_exe=build_options))

谢谢,那确实起作用了,不幸的是现在出现了其他错误。暂时专注于pyinstaller,但还是感谢您的帮助。 - Praxis
我有同样的问题。尽管当我测试你的代码时,我得到了以下信息 ImportError: No module named 'C:\\***\\***\\Python27\\lib\\site-packages\\scipy - Bridgekeeper
dirname()指的是os.path.dirname(),我猜想。你是如何“将includefiles添加到buildExe参数列表中”的? - Niko Pasanen
@np8,是的,dirname() 指的是 os.path.dirname()。我已经更新了我的答案,请参考 http://cx-freeze.readthedocs.io/en/latest/distutils.html - fepzzz

1
我遇到了同样的问题,并使用fepzzz的方法解决了它,同时包含了一些缺失的包:
additional_mods = ['numpy.matlib', 'multiprocessing.process']
includefiles = [(r'C:\Anaconda3\Lib\site-packages\scipy')]

setup(xxx, options={'build_exe': {'includes': additional_mods, 'include_files': includefiles}})

使用cx-Freeze软件包的5.0.2版本,解决了导入multiprocessing.process时出现的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接