Unidata / MetPy

MetPy is a collection of tools in Python for reading, visualizing and performing calculations with weather data.
https://unidata.github.io/MetPy/
BSD 3-Clause "New" or "Revised" License
1.26k stars 415 forks source link

AttributeError: module 'cupy' has no attribute 'ndarray'. #3213

Closed tzaremb2 closed 1 year ago

tzaremb2 commented 1 year ago

What went wrong?

Hello,

I keep running into this error code when I try to read in Metpy. I am running Python 3.11.0. AttributeError: module 'cupy' has no attribute 'ndarray'. I tried pip install cupy but kept getting errors in the install process. I was wondering if anybody had experienced this or had any insights on how to get around this. Any help would be much appreciated! Thanks!

Best, Troy

Operating System

MacOS

Version

1.5

Python Version

3.11.0

Code to Reproduce

import metpy

Errors, Traceback, and Logs

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 21
     17 import pandas as pd
     19 import glob
---> 21 import metpy.calc as mpcalc
     22 from metpy.units import units
     24 import wrf

File ~/anaconda3/lib/python3.11/site-packages/metpy/__init__.py:15
     12 os.environ['PINT_ARRAY_PROTOCOL_FALLBACK'] = '0'
     14 from ._version import get_version  # noqa: E402
---> 15 from .xarray import *  # noqa: F401, F403, E402
     17 __version__ = get_version()
     18 del get_version

File ~/anaconda3/lib/python3.11/site-packages/metpy/xarray.py:31
     29 from . import _warnings
     30 from ._vendor.xarray import either_dict_or_kwargs, expanded_indexer, is_dict_like
---> 31 from .units import (_mutate_arguments, DimensionalityError, is_quantity, UndefinedUnitError,
     32                     units)
     34 __all__ = ('MetPyDataArrayAccessor', 'MetPyDatasetAccessor', 'grid_deltas_from_dataarray')
     35 metpy_axes = ['time', 'vertical', 'y', 'latitude', 'x', 'longitude']

File ~/anaconda3/lib/python3.11/site-packages/metpy/units.py:26
     23 import warnings
     25 import numpy as np
---> 26 import pint
     28 log = logging.getLogger(__name__)
     30 UndefinedUnitError = pint.UndefinedUnitError

File ~/anaconda3/lib/python3.11/site-packages/pint/__init__.py:28
     16 from importlib.metadata import version
     18 from .errors import (  # noqa: F401
     19     DefinitionSyntaxError,
     20     DimensionalityError,
   (...)
     26     UnitStrippedWarning,
     27 )
---> 28 from .formatting import formatter, register_unit_format
     29 from .registry import ApplicationRegistry, LazyRegistry, UnitRegistry
     30 from .util import logger, pi_theorem  # noqa: F401

File ~/anaconda3/lib/python3.11/site-packages/pint/formatting.py:20
     17 from collections.abc import Iterable
     18 from numbers import Number
---> 20 from .babel_names import _babel_lengths, _babel_units
     21 from .compat import babel_parse, HAS_BABEL
     23 if TYPE_CHECKING:

File ~/anaconda3/lib/python3.11/site-packages/pint/babel_names.py:11
      1 """
      2     pint.babel
      3     ~~~~~~~~~~
   (...)
      6     :license: BSD, see LICENSE for more details.
      7 """
      9 from __future__ import annotations
---> 11 from .compat import HAS_BABEL
     13 _babel_units: dict[str, str] = dict(
     14     standard_gravity="acceleration-g-force",
     15     millibar="pressure-millibar",
   (...)
    138     radian="angle-radian",
    139 )
    141 if not HAS_BABEL:

File ~/anaconda3/lib/python3.11/site-packages/pint/compat.py:207
    203 # Define location of pint.Quantity in NEP-13 type cast hierarchy by defining upcast
    204 # types using guarded imports
    206 try:
--> 207     from dask import array as dask_array
    208     from dask.base import compute, persist, visualize
    209 except ImportError:

File ~/anaconda3/lib/python3.11/site-packages/dask/array/__init__.py:4
      1 from __future__ import annotations
      3 try:
----> 4     from dask.array import backends, fft, lib, linalg, ma, overlap, random
      5     from dask.array.blockwise import atop, blockwise
      6     from dask.array.chunk_types import register_chunk_type

File ~/anaconda3/lib/python3.11/site-packages/dask/array/backends.py:8
      5 import numpy as np
      7 from dask.array import chunk
----> 8 from dask.array.core import Array
      9 from dask.array.dispatch import (
     10     concatenate_lookup,
     11     divide_lookup,
   (...)
     19     to_numpy_dispatch,
     20 )
     21 from dask.array.numpy_compat import divide as np_divide

File ~/anaconda3/lib/python3.11/site-packages/dask/array/core.py:37
     35 from dask.array import chunk
     36 from dask.array.chunk import getitem
---> 37 from dask.array.chunk_types import is_valid_array_chunk, is_valid_chunk_type
     39 # Keep einsum_lookup and tensordot_lookup here for backwards compatibility
     40 from dask.array.dispatch import (  # noqa: F401
     41     concatenate_lookup,
     42     einsum_lookup,
     43     tensordot_lookup,
     44 )

File ~/anaconda3/lib/python3.11/site-packages/dask/array/chunk_types.py:112
    109 try:
    110     import cupy
--> 112     register_chunk_type(cupy.ndarray)
    113 except ImportError:
    114     pass

AttributeError: module 'cupy' has no attribute 'ndarray'
akrherz commented 1 year ago

That code block should be a harmless no-op if cupy is not installed as the ImportError will be handled by the try/except block. You most have some quasi-broken cupy installed, which could either be something simple like a python file called cupy.py somewhere in your $PYTHONPATH / pwd. Attempting to pip install it will likely end in tears as these are very complex packages. You appear to be using anaconda, so use conda to install/remove cupy and see if that can get rid of your problem.

tzaremb2 commented 1 year ago

Thanks! That was the problem! I really appreciate your help! Was able to get it to work.