1

Whenever I try importing cfgrib it gives me runtime error that it could not load ecCodes library

import cfgrib

Here's the full error message

RuntimeError                              Traceback (most recent call last)
/tmp/ipykernel_6224/857012844.py in <module>
----> 1 import cfgrib

~/.local/lib/python3.8/site-packages/cfgrib/__init__.py in <module>
     17 
     18 # cfgrib core API depends on the ECMWF ecCodes C-library only
---> 19 from .cfmessage import CfMessage
     20 from .dataset import Dataset, DatasetBuildError, open_file, open_fileindex
     21 from .messages import FileStream, Message

~/.local/lib/python3.8/site-packages/cfgrib/cfmessage.py in <module>
     27 import numpy as np
     28 
---> 29 from . import abc, messages
     30 
     31 LOG = logging.getLogger(__name__)

~/.local/lib/python3.8/site-packages/cfgrib/messages.py in <module>
     26 
     27 import attr
---> 28 import eccodes  # type: ignore
     29 import numpy as np
     30 

~/.local/lib/python3.8/site-packages/eccodes/__init__.py in <module>
     13 import sys
     14 
---> 15 from .eccodes import *
     16 from .eccodes import __version__
     17 from .eccodes import bindings_version

~/.local/lib/python3.8/site-packages/eccodes/eccodes.py in <module>
     10 #
     11 #
---> 12 from gribapi import __version__
     13 from gribapi import bindings_version
     14 

~/.local/lib/python3.8/site-packages/gribapi/__init__.py in <module>
     11 #
     12 
---> 13 from .gribapi import *  # noqa
     14 from .gribapi import __version__
     15 from .gribapi import bindings_version

~/.local/lib/python3.8/site-packages/gribapi/gribapi.py in <module>
   2226 
   2227 
-> 2228 __version__ = grib_get_api_version()
   2229 
   2230 

~/.local/lib/python3.8/site-packages/gribapi/gribapi.py in grib_get_api_version()
   2216 
   2217     if not lib:
-> 2218         raise RuntimeError("Could not load the ecCodes library!")
   2219 
   2220     v = lib.grib_get_api_version()

RuntimeError: Could not load the ecCodes library!

I have installed cfgrib and ecCodes through pip

cfgrib                 0.9.9.1             
eccodes                1.4.0               
eccodes-python         0.9.9
Waqas_14
  • 31
  • 6
  • Reinforcing [Robert's answer](https://stackoverflow.com/a/70404319/3888719), I strongly recommend using conda or another virtual environment manager (but especially conda) to manage your environment for geospatial libraries. Conda will take care of non-python dependencies in addition to your python ones, and you can have multiple conflicting setups for different projects without issue by using conda envs. Without it, this is way too tough to manage on your own. – Michael Delgado Dec 18 '21 at 18:14

1 Answers1

2

As stated on pypi (https://pypi.org/project/eccodes/) the eccodes python package relies on the eccodes system library. Based on the error message, you do not have it installed.

It is probably easiest to install it using conda:

conda install -c conda-forge eccodes
Robert Wilson
  • 3,192
  • 11
  • 19
  • But I have it installed and it is shown when using pip list, looks like I need to switch to Conda – Waqas_14 Dec 19 '21 at 14:16
  • 1
    I think you have misunderstood my response. pip will install the python package for using eccodes. You appear to be missing the system library. The conda install line I provided will do that. Alternatively you can use conda to install the python package: https://anaconda.org/conda-forge/python-eccodes. That should handle system libraries also – Robert Wilson Dec 19 '21 at 17:10