Skip to content

BUG: remove numpy.f2py from excludedimports#26776

Merged
rgommers merged 1 commit into
numpy:mainfrom
yanwic:add-numpy-f2py-in-pyinstaller-hook
Jul 7, 2024
Merged

BUG: remove numpy.f2py from excludedimports#26776
rgommers merged 1 commit into
numpy:mainfrom
yanwic:add-numpy-f2py-in-pyinstaller-hook

Conversation

@yanwic
Copy link
Copy Markdown
Contributor

@yanwic yanwic commented Jun 21, 2024

We noticed that PyInstaller fails to correctly bundle our application when using SciPy 1.13.1 & NumPy 2.0.0

Scipy ends up trying to import numpy.f2py when using optimize.root_scalar resulting in a runtime ModuleNotFoundError:

Traceback (most recent call last):
  File "numpy_min_reproducible\__init__.py", line 2, in <module>
  File "scipy\__init__.py", line 147, in __getattr__
  File "importlib\__init__.py", line 126, in import_module
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\optimize\__init__.py", line 413, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\optimize\_optimize.py", line 35, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\linalg\__init__.py", line 205, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\linalg\_basic.py", line 13, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\linalg\_decomp.py", line 26, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\_lib\_util.py", line 18, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\_lib\_array_api.py", line 17, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "scipy\_lib\array_api_compat\numpy\__init__.py", line 1, in <module>
  File "numpy\__init__.py", line 372, in __getattr__
ModuleNotFoundError: No module named 'numpy.f2py'

A minimal reproducible script would be the following:

from scipy import optimize
optimize.root_scalar(lambda x: x,x0=1)

Tested with these dependencies:

python = ">=3.10,<3.13"
numpy = "^2.0.0"
pyinstaller = "^6.8.0"
scipy = "^1.13.1"

Scipy 1.13.1 end up trying to import this module, e.g. when using optimize.root_scalar
@rokm
Copy link
Copy Markdown

rokm commented Jun 23, 2024

For a bit of context (since we've been looking into this over at PyInstaller as well): in numpy 2.0.0, f2py submodule was added to __all__ attribute of the numpy module. Therefore, excluding numpy.f2py in the PyInstaller hook breaks code that performs

from numpy import *

Most notable example of this is array_api_compat.numpy from https://github.com/data-apis/array-api-compat, which is used by scipy as scipy._lib.array_api_compat (as seen in the OP's traceback).

(The same would have happened with numpy.distutils, except it was not added to __all__ due to deprecation).

Copy link
Copy Markdown
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @yanwic and @rokm.

Labeling for backporting as well, we'll probably do a 2.0.1 in the near future and this will be useful to include.

@rgommers rgommers merged commit d0a328c into numpy:main Jul 7, 2024
@rgommers rgommers added the 09 - Backport-Candidate PRs tagged should be backported label Jul 7, 2024
@rgommers rgommers added this to the 2.1.0 release milestone Jul 7, 2024
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Jul 7, 2024
munnich added a commit to munnich/LARS that referenced this pull request Jul 8, 2024
numpy/numpy#26776 currently prevents pyinstaller from working
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

5 participants