Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLD: fix use of non-default interpreter, improve f2py handling #20612

Merged
merged 6 commits into from May 10, 2024

Conversation

rgommers
Copy link
Member

@rgommers rgommers commented Apr 29, 2024

This PR does the following:

  • Add a CI job which installs both python3.10 (default, gets python3 alias) and python3.11 in Ubuntu 22.04
  • Change almost all calls to f2py to a generator, making the code in meson.build files much more concise
  • Ensure all .pyf files are consistently named, matching their import name (modulo an underscore). This is necessary to support the generator usage.
  • Add import shims for interpolate.dfitpack, which were still missing.
  • Fix test_extending.py test setup so it works when the meson that is found isn't in the active environment but the default python3 one.
  • Get rid of all direct invocations of the Python interpreter that we're building for (which was done through custom_target(..., command: [py3, ...]). This will improve the cross-compilation experience, since QEMU or similar is no longer needed to run the cross interpreter.
    • Part of this is invoking f2py as f2py rather than python -m f2py. f2py generates code that does not depend on the platform or Python version it's installed for (with one single exception for long-double on some platforms - when the code hits _selected_real_kind_func - that we should not be affected by). This allows running f2py as a regular code generator, just like cython.

Closes gh-20535.

@rgommers rgommers added enhancement A new feature or improvement Meson Items related to the introduction of Meson as the new build system for SciPy labels Apr 29, 2024
@github-actions github-actions bot added scipy.stats scipy.special scipy.io scipy.sparse.linalg scipy.linalg scipy.sparse scipy.optimize scipy.interpolate scipy.integrate scipy._lib Build issues Issues with building from source, including different choices of architecture, compilers and OS CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure Fortran Items related to the internal Fortran code base Cython Issues with the internal Cython code base labels Apr 29, 2024
@rgommers rgommers force-pushed the fix-nondefault-interpreter branch from 3a33dab to ca35f3f Compare May 2, 2024 19:28
This guards against issues with trying to use the `python3` interpreter
for anything that actually requires the Python interpreter we are
building for. In such a build config with multiple interpreters,
we need to be careful not to pick up the wrong one when invoking
`custom_target` or `run_command` in meson.build files.
This removes all remaining invocations of `py3` in meson.build files.

Not invoking the Python interpreter we're building for at all
helps with cross-compilation, since that interpreter typically
isn't able to run without the use of an emulator like QEMU.
This private namespace was already deprecated in the
`__getattr__` in `interpolate/__init__.py`, however since
the f2py-generated extension module `dfitpack` had to be moved
to `_dfitpack`, this .py file has to be added instead. Do so
with the exact same pattern as `fitpack.py` and co.
The `meson` executable isn't guaranteed to be installed for the same
Python interpreter as we're running the tests for (this may for example
happen in distro setups when we're testing a non-default Python).
Explicitly passing the Python interpreter we're testing for to Meson
via a native file should be a robust solution.
@rgommers rgommers force-pushed the fix-nondefault-interpreter branch from ca35f3f to 3c37112 Compare May 3, 2024 06:56
@rgommers rgommers marked this pull request as ready for review May 3, 2024 08:09
@rgommers rgommers added this to the 1.14.0 milestone May 3, 2024
@rgommers
Copy link
Member Author

rgommers commented May 3, 2024

Okay, this is happy now! This is a fairly significant build change and fixes some nontrivial issues, so I'd like to get it merged soon so it has a few weeks in main before 1.14.x is branched.

@lucascolley
Copy link
Member

@thalassemia maybe one for you to review?

py3.extension_module('_mvn',
[mvn_module, 'mvndst.f'],
#[mvn_module, 'mvndst.f'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Can remove.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. And thanks for the review!


- name: Build wheel and install
run: |
python3.11 -m build -wnx -Csetup-args=-Dblas=blas-atlas -Csetup-args=-Dlapack=lapack-atlas -Ccompile-args=-j2
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we using build -x because pyproject.toml requires numpy>=2.0.0rc1?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes indeed. -n and -x almost always go together, because usually you're skipping build isolation to target an environment that is somehow different from what you'd get from build isolation.

Copy link
Contributor

@thalassemia thalassemia left a comment

Choose a reason for hiding this comment

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

Using a single F2PY generator is a great idea. Other than a commented line that can be removed, this LGTM!

scipy/meson.build Outdated Show resolved Hide resolved
Addresses review comments on PR 20612.

[skip ci]
@rgommers
Copy link
Member Author

The only change post-review-approval is a tweak to code comments, so I'll hit the green button here. Thanks again @thalassemia

@rgommers rgommers merged commit 686e303 into scipy:main May 10, 2024
@rgommers rgommers deleted the fix-nondefault-interpreter branch May 10, 2024 13:56
@czgdp1807
Copy link
Member

I got the following errors on main,

scipy/meson.build:156:15: ERROR: Command `/Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/bin/f2py -v` failed with status 1.
Program f2py found: YES (/Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/bin/f2py)
Running command: /Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/bin/f2py -v
--- stdout ---

--- stderr ---
Traceback (most recent call last):
  File "/Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/bin/f2py", line 5, in <module>
    from numpy.f2py.f2py2e import main
  File "/Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/lib/python3.11/site-packages/numpy/f2py/__init__.py", line 14, in <module>
    import subprocess
  File "/Users/czgdp1807/mambaforge/envs/scipy-dev-clang-15/lib/python3.11/subprocess.py", line 49, in <module>
    import signal
  File "/Users/czgdp1807/Quansight/scipy/scipy/signal/__init__.py", line 307, in <module>
    from . import _sigtools, windows
  File "/Users/czgdp1807/Quansight/scipy/scipy/signal/windows/__init__.py", line 42, in <module>
    from ._windows import *
  File "/Users/czgdp1807/Quansight/scipy/scipy/signal/windows/_windows.py", line 7, in <module>
    from scipy import linalg, special, fft as sp_fft
ModuleNotFoundError: No module named 'scipy'

After reverting this PR locally the above errors are removed.

@steppi
Copy link
Contributor

steppi commented May 13, 2024

I'm seeing the same error as @czgdp1807 when building SciPy on main using python dev.py build on Linux and have also found that I can still build when reverting this PR locally.

@rgommers
Copy link
Member Author

The traceback indicates that from within the stdlib's subprocess module the import signal statement is picking up scipy.signal rather than the stdlib's signal module. That is odd, and doesn't occur in any CI jobs. Do you have PYTHONPATH set or some such thing? If not, could you one of you please open a new issue with all the details about build environment and full build log?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build issues Issues with building from source, including different choices of architecture, compilers and OS CI Items related to the CI tools such as CircleCI, GitHub Actions or Azure enhancement A new feature or improvement Fortran Items related to the internal Fortran code base Meson Items related to the introduction of Meson as the new build system for SciPy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: generate_f2py mod is called by the wrong interpreter
5 participants