pythoncondaxesmf

"ModuleNotFoundError: No module named 'ESMF'" when importing xesmf installed via conda


I have tried to install xESMF module developed by pangeo-data collective into my conda environment with

conda install -c conda-forge --override-channels xesmf

however when I try to import xesmf I get the following error:

import xesmf as xe

Traceback (most recent call last):                                                                                   
  File "<python-input-0>", line 1, in <module>                                                                       
    import xesmf                                                                                                     
  File "/opt/conda/envs/geo/lib/python3.9/site-packages/xesmf/__init__.py", line 4, in <module>   
    from . frontend import Regridder                                                                                 
  File "/opt/conda/envs/geo/lib/python3.9/site-packages/xesmf/frontend.py", line 10, in <module>  
    from . backend import (esmf_grid, esmf_locstream, add_corner,                                                    
                           esmf_regrid_build, esmf_regrid_finalize)                                                  
  File "/opt/conda/envs/geo/lib/python3.9/site-packages/xesmf/backend.py", line 19, in <module>   
    import ESMF                                                                                                      
ModuleNotFoundError: No module named 'ESMF'                      

Indeed module ESMF is not available neither in conda default channels, nor in conda-forge.

This error has been reported in one of xesmf code repositories. https://github.com/JiaweiZhuang/xESMF/issues/131

However the only solution suggested on that thread was to create a new environment. Which I would prefer not to do as my current environment has many packages installed and recreating it with different package versions would require quite some integration testing for new versions.


Solution

  • The solution appears to be to ensure that conda install xesmf version 0.7 or higher. Prior to this version xesmf dependent on pyESMF that is not available via conda. Since version 0.7 xesmf switched to esmpy iand uses pyESMF only if esmpy is not available.

    try:
        import esmpy as ESMF
    except ImportError:
        import ESMF
    

    Here is the link to the relevant commit
    So the correct conda command to install xesmf would be:

    conda install -c conda-forge --override-channels "xesmf>=0.7"