使用py2exe打包Scipy

16

我在64位机器上使用python v2.7.3,scipy v0.11.0和py2exe v0.6.10,使用Christoph Gohlke提供的64位软件包时,遇到以下错误信息。如果有人能提供相关和有用的建议,我将不胜感激。以下是错误信息:

Traceback (most recent call last):
  File "test2.py", line 4, in <module>
  File "scipy\sparse\__init__.pyo", line 191, in <module>
  File "scipy\sparse\csgraph\__init__.pyo", line 146, in <module>
  File "scipy\sparse\csgraph\_shortest_path.pyo", line 12, in <module>
  File "scipy\sparse\csgraph\_shortest_path.pyo", line 10, in __load
  File "_shortest_path.pyx", line 18, in init scipy.sparse.csgraph._shortest_path (scipy\sparse\csgraph\_shortest_path.c:14235)
ImportError: No module named _validation

在一台旧的32位笔记本电脑上编译和运行可执行文件成功了(使用32位版本的所有内容),所以我认为我可能没有包含我需要的所有内容。我的新创建的test2.exe文件能够正确地创建并显示与scipy的入门页面上展示的相同图形。这是我的测试脚本:

# test2.py
# code is from the scipy web site example and works in Idle

from scipy import sparse
from scipy import optimize
from scipy import special
from numpy import *
from pylab import *

x = arange(0,10,0.01)
for k in arange(0.5,5.5):
     y = special.jv(k,x)
     plot(x,y)
     f = lambda x: -special.jv(k,x)
     x_max = optimize.fminbound(f,0,6)
     plot([x_max], [special.jv(k,x_max)],'ro')
title('Different Bessel functions and their local maxima')
show()

以下是我的setup.py文件:

# setup.py
from distutils.core import setup
import py2exe
import os
import matplotlib
setup(
    windows=[{'script': r'test2.py'}],
    data_files = matplotlib.get_py2exe_datafiles(),
    options = {
        'py2exe': {
            r'compressed': True,
            r'optimize': 2,
            r'includes': [
                r'matplotlib',
                r'matplotlib.backends.backend_tkagg',
                r'matplotlib.pyplot',
                #r'mpl_toolkits',
                r'pytz'
                ],
            r'dll_excludes': [r'MSVCP90.dll'],
            r'excludes': [
                '_gtkagg',
                '_tkagg',
                '_agg2',
                '_cairo',
                '_cocoaagg',
                '_fltkagg',
                '_gtk',
                '_gtkcairo',
                'tcl'
                ]
            }
        },
    )
os.system("pause")  # leaves the command prompt box open so I can read it

test2.py和setup.py均位于c:\python27\目录下,在64位机器上成功编译出test2.exe。在(可能)相关的说明中,我可以看出scipy v0.11.0引入了新的稀疏图形工具,我怀疑这就是错误消息要指向的地方。我是否漏掉了需要明确包含的某些内容?如果scipy也能像 matplotlib 一样提供一个 get_py2exe_datafiles() 函数来帮助正确打包东西,那就太好了。

预先感谢您提供的任何帮助,并感谢您阅读到这里。


bundle_files=3 也没有帮助 - chandradog
2个回答

15

据讨论这里,这似乎是py2exe和pyinstaller与scipy 0.11.0共同的问题。

该帖子中提供的临时解决方案是手动导入文件:

将以下代码添加到您的程序中

def dependencies_for_myprogram():
    from scipy.sparse.csgraph import _validation

问题已解决,适用于pyInstaller和py2exe。

你也可以尝试使用“includes”将此文件包含进去。这样对于py2exe来说就足够了。


13
问题已解决!非常感谢 joaquin。在搜寻了两天后,我没有找到 pyinstaller 链接。对于未来的读者,在 py2exe 的选项中,我添加了以下内容:
scipy.sparse.csgraph._validation

到includes目录下。


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