Pyinstaller 打包 scipy相关模块 dll load failed 解决

Pyinstaller 打包 scipy相关模块 dll load failed 解决。

报错:(大体信息)

scipy/linalg/blas.py
can't import _fblas
dll load failed

build 信息

5250 WARNING: lib not found: _pywrap_tensorflow_internal.pyd dependency of D:\Software\anaconda3\envs\ae_tf\lib\site-packages\tensorflow\python\util\_pywrap_tensor_float_32_execution.pyd
5265 WARNING: lib not found: _pywrap_tensorflow_internal.pyd dependency of D:\Software\anaconda3\envs\ae_tf\lib\site-packages\tensorflow\python\util\_pywrap_nest.pyd
5265 WARNING: lib not found: _pywrap_tensorflow_internal.pyd dependency of D:\Software\anaconda3\envs\ae_tf\lib\site-packages\tensorflow\python\util\_pywrap_utils.pyd
5328 WARNING: lib not found: _pywrap_tensorflow_internal.pyd dependency of D:\Software\anaconda3\envs\ae_tf\lib\site-packages\tensorflow\python\grappler\_pywrap_tf_cluster.pyd

环境

python == 3.9.12
scipy == 1.9.3
numpy == 1.19.5
查看 pyinstaller hook-scipy.py

# windows proper and in .libs if installed on a virtualenv created from MinGW (Git-Bash for example)
if is_win:
    extra_dll_locations = ['extra-dll', '.libs']
    for location in extra_dll_locations:
        dll_glob = os.path.join(os.path.dirname(get_module_file_attribute('scipy')), location, "*.dll")
        if glob.glob(dll_glob):
            binaries.append((dll_glob, "."))

# Handle delvewheel-enabled win32 wheels, which have external scipy.libs directory (scipy >= 0.9.2)
if is_module_satisfies("scipy >= 1.9.2") and is_win:
    datas, binaries = collect_delvewheel_libs_directory('scipy', datas=datas, binaries=binaries)

# collect library-wide utility extension modules
hiddenimports = ['scipy._lib.%s' % m for m in ['messagestream', "_ccallback_c", "_fpumode"]]

测试代码

测试以下代码,问题复现,直接运行程序ok, pyinstaller 打包后报错。

import numpy as np
from scipy import linalg

a = np.array([[1., 2.], [3., 4.]])

print(linalg.inv(a))

问题

scipy 1.9.2 以上 在site-packages 环境里有scipy.libs文件夹,而在scipy文件夹里无.libs文件夹。pyinstaller 找不到相关dll文件。

解决方案

降级scipy到1.9.1, scipy文件夹中有.libs文件夹。

pip install --upgrade scipy==1.9.1

但build还是显示缺少之前scipy.libs 中的dll文件。复制scipy==1.9.3版本中scipy.libs中dll文件到1.9.1中scipy/.libs文件夹。 solved.

讨论

pyinstaller hook 没有把 scipy dll 相关文件都正确打包,或许可以自己生成hook文件打包。

参考

https://github.com/pyinstaller/pyinstaller/issues/5197

你可能感兴趣的:(scipy,python,tensorflow)