ImportError when using Python Anaconda package grib_api

3k Views Asked by At

I am trying to use the ECMWF GRIB_API to access GRIB files from Python. I get this error

Traceback (most recent call last):
  File "/home/martin/markj/JustImportGRIB.py", line 1, in <module>
    from gribapi import *
ImportError: No module named gribapi

One line of code :)

from gribapi import *

I am using Anaconda Python 2.7.12, Linux Centos 64-bit, installed as user and not added Anaconda to the path if that's relevant - the system Python (2.6) is on the path. I installed GRIB_API from the Pingu Carsti channel which was the top Google hit. The conda install did not report any errors.

[martin@bonnie ~]$ anaconda2/bin/conda install -c pingucarsti grib_api=1.10.4
Fetching package metadata .........
Solving package specifications: ..........

Package plan for installation in environment /home/martin/anaconda2:

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    grib_api-1.10.4            |                3         1.7 MB  pingucarsti

The following NEW packages will be INSTALLED:

    grib_api: 1.10.4-3 pingucarsti

Proceed ([y]/n)? y

Fetching packages ...
grib_api-1.10. 100% |################################| Time: 0:00:01   1.24 MB/s
Extracting packages ...
[      COMPLETE      ]|###################################################| 100%
Linking packages ...
[      COMPLETE      ]|###################################################| 100%

I found some bug reports about this error being caused by a bug in the ECMWF GRIB_API, but I don't know enough about Anaconda (or perhaps Python) to figure out how to apply the workarounds.

2

There are 2 best solutions below

1
On

I read GRIB files in Python with pygrib. I have successfully installed it inside a conda virtual environment (Miniconda2 on Ubuntu 16.04). Here is what has worked for me:

  1. Install system dependencies for pygrib:

    sudo apt-get install libjpeg9

    sudo apt-get install libgrib-api-dev

  2. Install pygrib and ecmwf_grib from conda-forge channel.

    conda install -c conda-forge pygrib ecmwf_grib

0
On

Try conda-forge's build at python-eccodes (note: that's not eccodes which is the C library).

How it is built: https://github.com/conda-forge/python-eccodes-feedstock/blob/master/recipe/meta.yaml

The ECMWF's ecCodes library is the next version of their GRIB API, and is extremely similar to the old GRIB API.

pelson> conda create -n gribby -c conda-forge python-eccodes
Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment /Users/pelson/miniconda/envs/gribby:

The following NEW packages will be INSTALLED:

    ca-certificates: 2017.7.27.1-0         conda-forge
    curl:            7.54.1-0              conda-forge
    eccodes:         2.4.0-0               conda-forge
    hdf4:            4.2.12-0              conda-forge
    hdf5:            1.8.18-1              conda-forge
    intel-openmp:    2018.0.0-h68bdfb3_7   defaults   
    jasper:          1.900.1-4             conda-forge
    jpeg:            9b-1                  conda-forge
    krb5:            1.14.2-0              conda-forge
    libgfortran:     3.0.1-h93005f0_2      defaults   
    libnetcdf:       4.4.1.1-8             conda-forge
    libpng:          1.6.28-1              conda-forge
    libssh2:         1.8.0-1               conda-forge
    mkl:             2018.0.0-h5ef208c_6   defaults   
    ncurses:         5.9-10                conda-forge
    numpy:           1.13.3-py27h62f9060_0 defaults   
    openssl:         1.0.2l-0              conda-forge
    python:          2.7.14-0              conda-forge
    python-eccodes:  2.4.0-py27_1          conda-forge
    readline:        6.2-0                 conda-forge
    sqlite:          3.13.0-1              conda-forge
    tk:              8.5.19-2              conda-forge
    zlib:            1.2.8-3               conda-forge
#
# To activate this environment, use:
# > source activate gribby
#
# To deactivate an active environment, use:
# > source deactivate
#


pelson> source activate gribby
python(gribby) pelson> python
Python 2.7.14 | packaged by conda-forge | (default, Oct  5 2017, 23:08:53) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gribapi
>>> gribapi.__version__
'2.4.0'