-
Notifications
You must be signed in to change notification settings - Fork 89
Description
I am trying to run ESMPy on a Jupyter Hub system with ESMPy 8.8.1 (and ESMF 8.8.1) installed in the base Conda environment. My notebook uses a second conda environment with ESMPy/ESMF 8.9.1 installed. If I run Python code in the terminal in the second environment with 8.9.1 installed everything is ok, but if I try to run a Jupyter notebook which imports ESMPy I get a Version Mismatch error saying:
ESMF installation version 8.8.1 differs from ESMPy version 8.9.1
despite having 8.9.1 installed in this environment.
The root cause of this appears to be that line 25 of loadESMF.py reads the environment variable ESMFMKFILE. When I run in a Jupyter notebook the ESMFMKFILE environment variable is set to /opt/conda/lib/esmf.mk regardless of what conda environment the notebook is run in. When run from the terminal it picks up the path to esmf.mk from the conda environment.
As a work around I can manually set the os.environ['ESMFMKFILE'] in the Jupyter notebook or this code checks if the path of the Python interpreter differs from the path of the ESMFMKFILE.
import os, sys
# get the path of our python interpreter, e.g. /opt/conda/bin/python or /home/user/my_conda_env/bin/python
# this should return something like /opt/conda or /home/user/my_conda_env/
python_path = os.path.dirname(os.path.dirname(sys.executable))
# get the path of the ESMF mk file, e.g. /opt/conda/lib/esmf.mk or /home/user/my_conda_env/lib/esmf.mk
# this should also return something like /opt/conda or /home/user/my_conda_env/
esmf_path = os.path.dirname(os.path.dirname(os.environ["ESMFMKFILE"]))
# the python interpreter and ESMF mk file should share the same parent directory, if they don't then use the python interpreter path to set the ESMFMKFILE path.
if python_path != esmf_path:
os.environ["ESMFMKFILE"] = python_path + "/lib/esmf.mk"
# the import os esmpy should now succeed in all cases
import esmpy
If this was inserted around line 25 of src/addon/src/addon/src/loadESMPF.py (https://github.com/esmf-org/esmf/blob/develop/src/addon/esmpy/src/esmpy/interface/loadESMF.py) then it would solve the problem.