NCAR / wrf-python

A collection of diagnostic and interpolation routines for use with output from the Weather Research and Forecasting (WRF-ARW) Model.
https://wrf-python.readthedocs.io
Apache License 2.0
391 stars 150 forks source link

error when install wrf-python #237

Open Kevinello opened 2 months ago

Kevinello commented 2 months ago

Error when i use pip to install wrf-python:

Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting wrf-python
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/19/b9/ac6886e7995a9c83f393f758fd4803ab13bda0e57f8d3540f6d3a9f070db/wrf-python-1.3.4.1.tar.gz (1.1 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [18 lines of output]
      /root/.cache/pypoetry/virtualenvs/python-plot-u4OVFNWo-py3.8/bin/python: No module named pip
      Traceback (most recent call last):
        File "/root/.cache/pypoetry/virtualenvs/python-plot-u4OVFNWo-py3.8/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/root/.cache/pypoetry/virtualenvs/python-plot-u4OVFNWo-py3.8/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/root/.cache/pypoetry/virtualenvs/python-plot-u4OVFNWo-py3.8/lib/python3.8/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-_nr8l14r/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/tmp/pip-build-env-_nr8l14r/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-_nr8l14r/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 487, in run_setup
          super().run_setup(setup_script=setup_script)
        File "/tmp/pip-build-env-_nr8l14r/overlay/lib/python3.8/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 17, in <module>
      ModuleNotFoundError: No module named 'numpy'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

I try to install it through source code referencing to documentation, but it still raised the same error

And i'm 100% percent sure that i have numpy in my virtualenv(created by poetry), and i run pip install wrf-python while the virtualenv is activated(in the poetry shell)

image
omdaniel commented 2 months ago

I haven't examined the source code but from my own testing I surmise a couple of things going on here.

First, wrf-python does not play nice with build isolation. If you look up the stack trace you will see "No module named pip"

In my case I am using uv and I disable build isolation with --no-build-isolation and then the legacy installer will complain that I need pip installed, and then it will complain that I need wheel installed, and lastly on Windows without compilers will fail when it attempts to compile (if you have "Microsoft C++ Build Tools" on Windows it should succeed here, or if you are on Linux it should succeed here). However, requiring pip and wheel in your virtual environment is inconvenient and will cause issues with many modern tools that assume the packages can be installed with build isolation.

Second, wrf-python still relies on distutils, deprecated after numpy 1.23 and fully removed in 1.26. So to even get as far as the above suggestion will get, you need to install numpy<1.26

I would also say that #42 precompiled wheels issues open since 2017 would likely resolve these issues

DWesl commented 1 month ago

I haven't examined the source code but from my own testing I surmise a couple of things going on here.

First, wrf-python does not play nice with build isolation. If you look up the stack trace you will see "No module named pip"

https://github.com/NCAR/wrf-python/blob/cec1f70ceff9e4e051e4e0ed73104c60220ab0eb/setup.py#L14

In my case I am using uv and I disable build isolation with --no-build-isolation and then the legacy installer will complain that I need pip installed, and then it will complain that I need wheel installed, and lastly on Windows without compilers will fail when it attempts to compile (if you have "Microsoft C++ Build Tools" on Windows it should succeed here, or if you are on Linux it should succeed here). However, requiring pip and wheel in your virtual environment is inconvenient and will cause issues with many modern tools that assume the packages can be installed with build isolation.

I thought pip only enabled build isolation if it found pyproject.toml, has that changed recently?

EDIT: Just re-read the text, using uv instead of pip.

The failure without compilers is expected, wrf-python not putting numpy into setuptools's old setup_requires is less so, though, as noted above, it will try to install NumPy if not present. It needs NumPy installed before the part of the setup script where it could specify NumPy as a requirement, so there wasn't really a good solution before pyproject.toml.

EDIT: Workarounds: comment out line 14 of setup.py above, ensure you have numpy, setuptools, and wheel installed in the build environement, and disable build isolation. Alternately, use the pyproject.toml and the setup.py changes from #241 to allow build isolation for WRF-python on python<3.12.

Second, wrf-python still relies on distutils, deprecated after numpy 1.23 and fully removed in 1.26. So to even get as far as the above suggestion will get, you need to install numpy<1.26

Specifically on NumPy distutils, which looks like it's around for another year on python 3.11 and will never be on 3.12, so there's an argument for python_requires="<3.12" in the setup script (#240). This is #217 on this repository.

That link proposes suggestions for migrating the build for python 3.12, specifically meson through meson-python or CMake using scikit-build-core, if anyone wants to try for a PR (#242 for CMake).