Search code examples
pythonpython-3.xnumpyhdf

Error: numpy.core.multiarray failed to import pyhdf


I'm trying to open HDF files using a python code provided in this website (https://hdfeos.org/software/pyhdf.php). However, I get an error while importing the pyhdf package

import os
from pyhdf.SD import SD, SDC
import numpy as np
import rasterio

The error :

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
RuntimeError: module compiled against API version 0x10 but this version of numpy is 0xf . Check the section C-API incompatibility at the Troubleshooting ImportError section at https://numpy.org/devdocs/user/troubleshooting-importerror.html#c-api-incompatibility for indications on how to solve this problem .
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [14], in <cell line: 2>()
      1 import os
----> 2 from pyhdf.SD import SD, SDC
      3 import rasterio

File ~\Miniconda3\lib\site-packages\pyhdf\SD.py:1003, in <module>
     38 """
     39 SD (scientific dataset) API (:mod:`pyhdf.SD`)
     40 =============================================
   (...)
    999 
   1000 """
   1001 import os, sys, types
-> 1003 from . import hdfext as _C
   1004 from .six.moves import xrange
   1005 from .error import _checkErr, HDF4Error

File ~\Miniconda3\lib\site-packages\pyhdf\hdfext.py:10, in <module>
      8 # Import the low-level C/C++ module
      9 if __package__ or "." in __name__:
---> 10     from . import _hdfext
     11 else:
     12     import _hdfext

ImportError: numpy.core.multiarray failed to import

I tried upgrading numpy and pyhdf versions but it didn't work


Solution

  • As commented by @Cow, I am posting this as an answer:

    The following packages are required to build and install pyhdf:
    
    Python: Python 2.6 or newer for Python 2, or Python 3.2 or newer for Python 3.
    
    NumPy
    
    HDF4 libraries (to use their HDF4 binaries, you will also need szip, available from the same page)
    
    Compiler suite e.g. GCC. On Windows, you need to use a compatible Visual C++ compiler.
    
    zlib
    
    libjpeg
    

    https://fhs.github.io/pyhdf/install.html#requirements


    Pay special attention to Swig-generated interface files

    Interface files hdfext.py and hdfext_wrap.c (located under the pyhdf subdirectory) have been generated using the SWIG tool. Those two files should be usable as is on most environments. It could happen however that, for reasons related to your environment, your C compiler does not accept the ‘.c’ file and raises a compilation error. If so, the interface needs to be regenerated. To do so, install SWIG, then run:

    $ cd pyhdf
    $ swig -python hdfext.i
    

    SWIG should silently regenerate the two interface files, after which installation should proceed correctly.

    https://fhs.github.io/pyhdf/install.html#swig-generated-interface-files