ecmwf / cfgrib

A Python interface to map GRIB files to the NetCDF Common Data Model following the CF Convention using ecCodes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot find eccodes when using cfgrib

happymvd opened this issue · comments

I am working on a Windows 11 computer, and I am very new to developing in python
I writing a python system that needs to convert grib2 files to NetCDF files.
I am working in a virtual environment and using Visual studio Code.
I have eccodes (1.7.0) and cfgrib(0.9.10.4) installed within my virtual environment.
My code imports cfgrib, but when I run the code I get the following error message -

Traceback (most recent call last):
File "C:\Data\GitHub\SearchWell\GetForecast.py", line 7, in
import cfgrib
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\cfgrib_init_.py", line 20, in
from .cfmessage import COMPUTED_KEYS
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\cfgrib\cfmessage.py", line 29, in
from . import abc, messages
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\cfgrib\messages.py", line 28, in
import eccodes # type: ignore
^^^^^^^^^^^^^^
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\eccodes_init_.py", line 13, in
from .eccodes import * # noqa
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\eccodes\eccodes.py", line 12, in
from gribapi import (
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\gribapi_init_.py", line 13, in
from .gribapi import * # noqa
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\gribapi\gribapi.py", line 34, in
from gribapi.errors import GribInternalError
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\gribapi\errors.py", line 16, in
from .bindings import ENC, ffi, lib
File "C:\Data\GitHub\SearchWell\VirtualEnvironment\Lib\site-packages\gribapi\bindings.py", line 35, in
raise RuntimeError("Cannot find the ecCodes library")
RuntimeError: Cannot find the ecCodes library

The same happens when I try and import pygrib

When I use pipdeptree to see the dependencies I get the following -
(VirtualEnvironment) PS C:\Data\GitHub\SearchWell> pipdeptree
cfgrib==0.9.10.4
├── attrs [required: >=19.2, installed: 23.2.0]
├── click [required: Any, installed: 8.1.7]
│ └── colorama [required: Any, installed: 0.4.6]
├── eccodes [required: >=0.9.8, installed: 1.7.0]
│ ├── attrs [required: Any, installed: 23.2.0]
│ ├── cffi [required: Any, installed: 1.16.0]
│ │ └── pycparser [required: Any, installed: 2.21]
│ ├── findlibs [required: Any, installed: 0.0.5]
│ └── numpy [required: Any, installed: 1.26.4]
└── numpy [required: Any, installed: 1.26.4]

Are there any suggestions as to how I can resolve this issue?

Hello @happymvd, in addition to cfgrib (or pygrib) you need to install the ecCodes binary library. Some installation instructions on doing this via conda-forge are here: https://confluence.ecmwf.int/display/UDOC/How+to+install+ecCodes+with+Python+bindings+in+conda+-+ecCodes+FAQ
You can then decide whether to fully use the conda environment for all your packages (might be easiest), or just use the ecCodes libraries from there while using your virtualenv.

Hello @happymvd, in addition to cfgrib (or pygrib) you need to install the ecCodes binary library. Some installation instructions on doing this via conda-forge are here: https://confluence.ecmwf.int/display/UDOC/How+to+install+ecCodes+with+Python+bindings+in+conda+-+ecCodes+FAQ You can then decide whether to fully use the conda environment for all your packages (might be easiest), or just use the ecCodes libraries from there while using your virtualenv.

Thank you @iainrussell ...... my knowledge and experience is minimal .....
I used the pip command to install eccodes as follows : pip install eccodes
My system said it had been installed, and when I looked at the dependencies for cfgrib I got this report -
cfgrib==0.9.10.4
├── attrs [required: >=19.2, installed: 23.2.0]
├── click [required: Any, installed: 8.1.7]
│ └── colorama [required: Any, installed: 0.4.6]
├── eccodes [required: >=0.9.8, installed: 1.7.0]
│ ├── attrs [required: Any, installed: 23.2.0]
│ ├── cffi [required: Any, installed: 1.16.0]
│ │ └── pycparser [required: Any, installed: 2.21]
│ ├── findlibs [required: Any, installed: 0.0.5]
│ └── numpy [required: Any, installed: 1.26.4]
└── numpy [required: Any, installed: 1.26.4]

I see a notable difference though .... you refer to ecCodes (with a capital) and I did not ..... is this an important omission on my part?

I seem to have solved my problem.
I deleted the virtual environment that I created using the terminal window in Visual Studio Code. Then I created a virtual environment through Conda and loaded the libraries, including eccodes and cfgrib, that I need for my system.
Now I can import cfgrib in my script with no problem

Fantastic, thanks for letting us know you've got a working environment! As a side-note, we are working on a simpler installation process.

I have recently found out that cfgrb works just fine in a non-Conda environment if one installs the ecwmflibs library (it installs eccodes) instead of installing eccodes directly.

Ah, that's true, I forgot to say that. It's good to have different options!