scipy with py2exe

chandradog picture chandradog · Jan 8, 2013 · Viewed 9k times · Source

I get the following error message using python v2.7.3 and scipy v0.11.0 with py2exe v0.6.10 on a 64 bit machine using 64 bit versions of the packages from Christoph Gohlke. If anyone can provide relevant and useful suggestions I would greatly appreciate it. Here is the error message:

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

Compiling and running the executable worked on an old 32 bit laptop (with 32 bit versions of everything) so I think I may not be including everything I need. My newly created test2.exe file properly creates and displays the same graph as shown at scipy's Getting Started page. Here is my test script:

# 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()

And here is my setup.py file:

# 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

Both test2.py and setup.py reside in c:\python27\ and I get a successfully compliled test2.exe on the 64 bit machine. On a (probably) related note, I can read that scipy v0.11.0 introduced new sparse graphing tools and I suspect this is where the error message is trying to point me to. Am I missing something I need to explicitly include? It would be nice if scipy had a get_py2exe_datafiles() function like matplotlib to help bundle things correctly.

Thank you in advance for any help you can provide, and for reading this far.

Answer

joaquin picture joaquin · Jan 8, 2013

This seems to be a problem common to py2exe and pyinstaller with scipy 0.11.0 as discussed here.

The temporal solution given in that thread is to import the file manually:

adding the following codes into your program

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

Problem solved for both pyInstaller and py2exe

You can alternatively try including this file with 'includes'. It should be enough for py2exe to get it.