py2exe 打包scipy时遇到的问题

时间:2022-08-28 11:33:53

最近写了个小程序,用PyQt5做的界面,写完之后用py2exe打包成独立的exe文件,运行正常。

后来由于需要,调用SciPy.io.loadmat,改写setup.py,打包之后运行错误,提示:

Traceback (most recent call last):
File
"comtrade.py", line 7, in <module>
File
"C:\Anaconda3\lib\site-packages\scipy\io\__init__.py", line 85, in <module>
from .matlab import loadmat, savemat, whosmat, byteordercodes
File
"C:\Anaconda3\lib\site-packages\scipy\io\matlab\__init__.py", line 13, in <module>
from .mio import loadmat, savemat, whosmat
File
"C:\Anaconda3\lib\site-packages\scipy\io\matlab\mio.py", line 12, in <module>
from .miobase import get_matfile_version, docfiller
File
"C:\Anaconda3\lib\site-packages\scipy\io\matlab\miobase.py", line 22, in <module>
from scipy.misc import doccer
File
"C:\Anaconda3\lib\site-packages\scipy\misc\__init__.py", line 47, in <module>
from scipy.special import comb, factorial, factorial2, factorialk
File
"C:\Anaconda3\lib\site-packages\scipy\special\__init__.py", line 586, in <module>
from ._ufuncs import *
File
"<loader>", line 10, in <module>
File
"<loader>", line 8, in __load
ImportError: (No module named
'scipy.special._ufuncs_cxx') 'D:\\work\\comtrade\\dist\\scipy.special._ufuncs.pyd'

可是找了下 \Anaconda3\Lib\site-packages\scipy\special里面,是有_ufuncs_cxx.pyd的,

也就是说py2exe在打包的时候没有把'scipy.special._ufuncs_cxx'打进来,需要在"includes" 里面加进去。

这个加进来之后,运行仍然会提示 (No module named "scipy.integrate")等,缺什么就一并"includes" 进来。

1 "includes" : [  'scipy.special._ufuncs_cxx',
2 "scipy.integrate",
3 "scipy.integrate.quadpack",
4 "scipy.sparse.csgraph._validation"],

最终setup.py如下:

 1 # -*- coding: utf-8 -*-
2 #py2exe:setup.py
3
4 from distutils.core import setup
5 import py2exe
6 #We need to import the glob module to search for all files.
7 import glob
8 import sys
13 #this allows to run it with a simple double click.
14 sys.argv.append('py2exe')
15
16 # We need to exclude matplotlib backends not being used by this executable. You may find
17 # that you need different excludes to create a working executable withyour chosen backend.
18 # We also need to include include various numerix libraries that the other functions call.
19 opts= {
20 'py2exe':{ "includes" : [ "sip", "matplotlib.backends", "matplotlib.backends.backend_tkagg",
21 "matplotlib.figure","numpy", "matplotlib.pyplot", "pylab", "six",
22 "matplotlib.backend_bases", 'scipy.special._ufuncs_cxx',
23 "scipy.integrate","scipy.integrate.quadpack","scipy.sparse.csgraph._validation"],
24 "excludes" : ['_gtkagg', '_tkagg', '_agg2', '_cairo', '_cocoaagg',
25 '_fltkagg','_gtk', '_gtkcairo'],
26
27 "dll_excludes":['libgdk-win32-2.0-0.dll','libgobject-2.0-0.dll',"MSVCP90.dll",]
28 }
29 }
30
31 #Save matplotlib-data to mpl-data ( It is located in thematplotlib\mpl-data
32 #folder and the compiled programs will look for it in \mpl-data
33 #note: using matplotlib.get_mpldata_info
34 data_files= [(r'mpl-data',glob.glob(r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\*.*')),
35 #Because matplotlibrc does not have an extension, glob does not findit (at least I think that's why)
36 #So add it manually here:
37 (r'mpl-data',[r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\matplotlibrc']),
38 (r'mpl-data\images',glob.glob(r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\images\*.*')),
39 (r'mpl-data\stylelib',glob.glob(r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\stylelib\*.*')),
40 (r'mpl-data\fonts',glob.glob(r'C:\Anaconda3\Lib\site-packages\matplotlib\mpl-data\fonts\*.*')),
41 ("",[r"C:\Anaconda3\Lib\site-packages\PyQt5\libEGL.dll"]),
42 ("platforms",[r"C:\Anaconda3\Lib\site-packages\PyQt5\plugins\platforms\qwindows.dll"])]
43
44 #for console program use 'console = [{"script" :"comtrade.py"}]
45 setup(
46 #console = [{"script" : 'comtrade.py'}],
47 name = 'comtrade',
48 version = '1.0',
49 windows = [{"script":"comtrade.py", "icon_resources": [(1, "logo.ico")]} ],
50 options=opts, data_files=data_files)

运行时仍然出错,提示(No module named "linalg"),找来找去也没找到 linalg.pyd这个文件,只好把有 import linalg的地方删掉,运行ok。