pythonmacos-mojave

How to read CDF file with python on mac?


I have installed spacepy using the following command (MacOS Mojave):

pip install git+https://github.com/spacepy/spacepy.git

But I keep getting this error while trying to from spacepy import pycdf:

Exception: Cannot load CDF C library; checked . Try 'os.environ["CDF_LIB"] = library_directory' before import.

I have tried following the directions from their website, but it seems like those directions are a bit outdated.


Solution

  • You can do something like:

    import os
    os.environ["CDF_LIB"] = "~/CDF/lib" # Give the path of the cdf library
    # e.g. in this case the library is in your home directory.
    

    Before doing:

    from spacepy import pycdf
    

    Source of the error can be found on this SpacePy website.

    The cdf library is available here.

    Basically, you need to set up the CDF_LIB (if pycdf has a trouble finding it) before importing the module. You can read the documentation here.