hook-scipy.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2023, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. # -----------------------------------------------------------------------------
  11. import glob
  12. import os
  13. import sysconfig
  14. from PyInstaller.compat import is_win, is_linux
  15. from PyInstaller.utils.hooks import (
  16. get_module_file_attribute,
  17. check_requirement,
  18. collect_delvewheel_libs_directory,
  19. collect_submodules,
  20. )
  21. binaries = []
  22. datas = []
  23. # Package the DLL bundle that official scipy wheels for Windows ship The DLL bundle will either be in extra-dll on
  24. # windows proper and in .libs if installed on a virtualenv created from MinGW (Git-Bash for example)
  25. if is_win:
  26. extra_dll_locations = ['extra-dll', '.libs']
  27. for location in extra_dll_locations:
  28. dll_glob = os.path.join(os.path.dirname(get_module_file_attribute('scipy')), location, "*.dll")
  29. if glob.glob(dll_glob):
  30. binaries.append((dll_glob, "."))
  31. # Handle delvewheel-enabled win32 wheels, which have external scipy.libs directory (scipy >= 0.9.2)
  32. if check_requirement("scipy >= 1.9.2") and is_win:
  33. datas, binaries = collect_delvewheel_libs_directory('scipy', datas=datas, binaries=binaries)
  34. # collect library-wide utility extension modules
  35. hiddenimports = ['scipy._lib.%s' % m for m in ['messagestream', "_ccallback_c", "_fpumode"]]
  36. # In scipy 1.14.0, `scipy._lib.array_api_compat.numpy` added a programmatic import of its `.fft` submodule, which needs
  37. # to be added to hiddenimports.
  38. # In scipy 1.18.0rc1, `scipy._lib.array_api_compat` was renamed to `scipy._external.array_api_compat`.
  39. if check_requirement("scipy >= 1.18.0rc1"):
  40. hiddenimports += ['scipy._external.array_api_compat.numpy.fft']
  41. elif check_requirement("scipy >= 1.14.0"):
  42. hiddenimports += ['scipy._lib.array_api_compat.numpy.fft']
  43. # If scipy is provided by Debian's python3-scipy, its scipy.__config__ submodule is renamed to a dynamically imported
  44. # scipy.__config__${SOABI}__
  45. # https://salsa.debian.org/python-team/packages/scipy/-/blob/1255922cf7c52b05aa44fb733449953cd9adb815/debian/patches/scipy_config_SOABI.patch
  46. if is_linux and "dist-packages" in get_module_file_attribute("scipy"):
  47. hiddenimports.append('scipy.__config__' + sysconfig.get_config_var('SOABI') + '__')
  48. # The `scipy._lib.array_api_compat.numpy` module performs a `from numpy import *`; in numpy 2.0.0, `numpy.f2py` was
  49. # added to `numpy.__all__` attribute, but at the same time, the upstream numpy hook adds `numpy.f2py` to
  50. # `excludedimports`. Therefore, the `numpy.f2py` sub-package ends up missing. Due to the way exclusion mechanism works,
  51. # we need to add both `numpy.f2py` and all its submodules to hiddenimports here.
  52. if check_requirement("numpy >= 2.0.0"):
  53. hiddenimports += collect_submodules('numpy.f2py', filter=lambda name: name != 'numpy.f2py.tests')
  54. # Starting with scipy 1.17.0, the `scipy._cyutility` extension is imported when top-level `scipy` package is imported
  55. # (via import of another extension, `scipy._lib`).
  56. if check_requirement("scipy >= 1.17.0"):
  57. hiddenimports += ['scipy._cyutility']