basemap: KeyError 'PROJ_LIB'

I install basemap with the command conda install basemap -c conda-forge

but

from mpl_toolkits.basemap import Basemap

returns:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-212c45f90d40> in <module>()
----> 1 from mpl_toolkits.basemap import Basemap
      2 #import matplotlib.pyplot as plt
      3 import numpy as np

/srv/conda/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in <module>()
    144 
    145 # create dictionary that maps epsg codes to Basemap kwargs.
--> 146 pyproj_datadir = os.environ['PROJ_LIB']
    147 epsgf = open(os.path.join(pyproj_datadir,'epsg'))
    148 epsg_dict={}

/srv/conda/lib/python3.6/os.py in __getitem__(self, key)
    667         except KeyError:
    668             # raise KeyError with the original key value
--> 669             raise KeyError(key) from None
    670         return self.decodevalue(value)
    671 

KeyError: 'PROJ_LIB'

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 32 (13 by maintainers)

Most upvoted comments

I can confirm this behavior with a clean install of basemap via conda into a new environment. You can see in the traceback that it refers to a conda environment:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 146, in <module>
    pyproj_datadir = os.environ['PROJ_LIB']
  File "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'PROJ_LIB'

PROJ_LIB does not appear anywhere in Basemap. This is a downstream change in the conda-forge package that should be discussed there. So I’m closing this issue since it is not Basemap related.

It seems the environment variable PROJ_LIB is not being set in the environment.

For python/ipython in a terminal, I managed to rectify by updating proj4:

$ conda create --name test --channel conda-forge python=3.6 basemap
$ conda activate test
(test) $ python -c "from mpl_toolkits.basemap import Basemap"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/anaconda3/envs/test/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 147, in <module>
    epsgf = open(os.path.join(pyproj_datadir,'epsg'))
FileNotFoundError: [Errno 2] No such file or directory: 'PROJ_LIB/epsg'

(test) $ conda list proj
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
proj4                     4.9.3                h470a237_8    conda-forge
pyproj                    1.9.5.1          py36h508ed2a_3    conda-forge

(test) $ conda list basemap
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
basemap                   1.1.0            py36h50ae964_6    conda-forge
basemap-data-hires        1.1.0                         0    conda-forge

(test) $ env | grep -i proj

(test) $ conda update --channel conda-forge proj4

(test) $ conda list proj
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
proj4                     5.1.0                h470a237_2    conda-forge
pyproj                    1.9.5.1                  py36_0    conda-forge

(test) $ conda list basemap
# packages in environment at /opt/anaconda3/envs/test:
#
# Name                    Version                   Build  Channel
basemap                   1.1.0            py36h50ae964_6    conda-forge

(test) $ python -c "from mpl_toolkits.basemap import Basemap"

(test) $ env | grep -i proj
_CONDA_SET_PROJ_LIB=PROJ_LIB
PROJ_LIB=/opt/anaconda3/envs/test/share/proj

However, the problem persisted inside Jupyter. I think they may have fixed it recently. I haven’t been able to test that pull yet.

In the meantime, a quick workaround (in your Jupyter kernel), for me at least, was to add the variable explicitly in my kernel.json file:

...
 "env": {
  "PROJ_LIB": "/opt/anaconda3/envs/test/share/proj",
...

Nonetheless, I’m not sure if the assumption of the presence of the environment variable in basemap/__init__.py:

pyproj_datadir = os.environ['PROJ_LIB']

should be there. It didn’t appear to be in the previous version (1.0.7).

Are you installing it on your root environment? If so the env vars won’t be activate, you must install it in an env:

conda create --name TEST python=3.6 basemap
conda activate TEST
python -c "from mpl_toolkits.basemap import Basemap"

PS: this is not an issue with basemap but actually how conda packages work.

this was the only way to resolve the KeyError ‘PROJ_LIB’, using this : https://gist.github.com/junzis/36fee36acd7cadfdf1c01c06d97f4947