JiaweiZhuang / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
269 stars 49 forks source link

AttributeError: module 'ESMF' has no attribute 'api' #19

Closed jgerardsimcock closed 6 years ago

jgerardsimcock commented 6 years ago

I've just installed the libraries as directed in the installation guide. When I import xesmf I get the following stack trace.

Should I also raise this upstream in the esmfpy channel?

JiaweiZhuang commented 6 years ago

Thanks for reporting. Problem confirmed. A workaround is making a softlink to libnetcdf.so in the conda environment (ref: modelica-3rdparty/netCDF-DataReader#15, conda/conda#3973)

The first time of running import ESMF should return a longer trace than what you've shown:

```python In [1]: import ESMF Traceback (most recent call last): File "/home/ubuntu/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/interface/loadESMF.py", line 118, in mode=ct.RTLD_GLOBAL) File "/home/ubuntu/miniconda/envs/geo/lib/python3.6/ctypes/__init__.py", line 348, in __init__ self._handle = _dlopen(self._name, mode) OSError: libnetcdf.so.11: cannot open shared object file: No such file or directory --------------------------------------------------------------------------- OSError Traceback (most recent call last) ~/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/interface/loadESMF.py in () 117 _ESMF = ct.CDLL(os.path.join(libsdir,'libesmf_fullylinked.so'), --> 118 mode=ct.RTLD_GLOBAL) 119 except: ~/miniconda/envs/geo/lib/python3.6/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error) 347 if handle is None: --> 348 self._handle = _dlopen(self._name, mode) 349 else: OSError: libnetcdf.so.11: cannot open shared object file: No such file or directory During handling of the above exception, another exception occurred: ImportError Traceback (most recent call last) in () ----> 1 import ESMF ~/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/__init__.py in () 54 #### IMPORT LIBRARIES ######################################################### 55 ---> 56 from ESMF.api.esmpymanager import * 57 from ESMF.api.grid import * 58 from ESMF.api.mesh import * ~/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/api/esmpymanager.py in () 9 from ESMF.api.constants import * 10 from ESMF.util.exceptions import * ---> 11 from ESMF.interface.cbindings import * 12 from ESMF.util.decorators import initialize 13 ~/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/interface/cbindings.py in () 11 import ESMF.api.constants as constants 12 from ESMF.util.decorators import deprecated, netcdf ---> 13 from ESMF.interface.loadESMF import _ESMF 14 15 def copy_struct(src): ~/miniconda/envs/geo/lib/python3.6/site-packages/ESMF/interface/loadESMF.py in () 119 except: 120 traceback.print_exc(file=sys.stdout) --> 121 raise ImportError('The ESMF shared library did not load properly.') ImportError: The ESMF shared library did not load properly. ```

In the above error message, notice the line:

OSError: libnetcdf.so.11: cannot open shared object file: No such file or directory

Go to the lib/ folder of your conda environment (mine is called geo, so the full path is ~/miniconda/envs/geo/lib)

$ ls libnetcdf*
libnetcdf.a  libnetcdff.a  libnetcdff.so  libnetcdff.so.6  libnetcdff.so.6.1.1  libnetcdf.settings  libnetcdf.so  libnetcdf.so.13

ESMPy is looking for libnetcdf.so.11 but it doesn't exist. So, simply create a new link:

$ ln -s libnetcdf.so libnetcdf.so.11

This solves my problem. Please see if it works for you. I will raise the issue upstream (or tell me if you already did that).

JiaweiZhuang commented 6 years ago

BTW, I can only reproduce this error on Ubuntu16.04, not on my Mac or using Docker-miniconda3. Seems like a system-specific issue.

JiaweiZhuang commented 6 years ago

Turns out that installing by conda install -c conda-forge esmpy (not the NESII channel) solves the problem on Ubuntu.

The doc is updated accordingly: 26a4bd1476e4cd48425d6e0c6e6a8aff4321b46c

jgerardsimcock commented 6 years ago

Thank you @JiaweiZhuang! This now works without issue. Installing esmpy from conda-forge channel was all that needed to be updated.