Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError 'PROJ_LIB' #2

Open
statiksof opened this issue Sep 3, 2018 · 9 comments
Open

KeyError 'PROJ_LIB' #2

statiksof opened this issue Sep 3, 2018 · 9 comments

Comments

@statiksof
Copy link

statiksof commented Sep 3, 2018

In the notebook BinderNotebook Executing

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

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'
@gaswani
Copy link

gaswani commented Oct 17, 2018

I am having the same issue:
KeyError Traceback (most recent call last)
in ()
9 from matplotlib.patches import Polygon
10 from matplotlib.collections import PatchCollection
---> 11 from mpl_toolkits.basemap import Basemap
12 from scipy import stats
13 import matplotlib.patches as mpatches

~/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/init.py in ()
153
154 # create dictionary that maps epsg codes to Basemap kwargs.
--> 155 pyproj_datadir = os.environ['PROJ_LIB']
156 epsgf = open(os.path.join(pyproj_datadir,'epsg'))
157 epsg_dict={}

~/anaconda3/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'

Anyone out there with an idea of how to resolve this problem?

@martinchristen
Copy link
Owner

This is a bug that happens with BaseMap (see [https://github.com/matplotlib/basemap/issues/419])

I fixed it on my Linux machine by installing:

conda install -c conda-forge basemap-data-hires

In future I'm not using BaseMap anymore, there are just too many problems. I switched to cartopy ( https://scitools.org.uk/cartopy/docs/v0.16/ ).

Next week I will have a talk at pycon.de and I will provide an updated notebook using cartopy (in a new repository called PyConDE2018). And at some point, I'll upgrade this notebook too.

@joefutrelle
Copy link

@martinchristen interesting, I tried that on my Linux machine with no luck. Maybe this is another version-sensitive issue?

@statiksof
Copy link
Author

@joefutrelle I opened this issue (conda-forge/basemap-feedstock#30) a month a ago. Maybe you find something interesting for your case.

@martinchristen
Copy link
Owner

@joefutrelle I checked the history on my Linux machine and I actually did the following:

conda remove basemap
conda install -c conda-forge basemap-data-hires:1.1.0-0

But as I mentioned, I moved away from basemap and I'm using cartopy now which is available using

conda install cartopy

@gaswani
Copy link

gaswani commented Oct 17, 2018

I managed to find a solution to this error. I found the solution at https://conda-forge.org/

In the bash terminal I first typed :

$ conda config --add channels conda-forge

Followed by:

$ conda install basemap

I thereafter ran my script in Juptyer notebook and the error was gone. I was able to plot the Chloropeth map of the world.

@jaecheverrip
Copy link

jaecheverrip commented Nov 1, 2018

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

@ReimarBauer
Copy link

Please use a conda environment. The PROJ_LIB is set if you are not do everything in the base environment. The base environment is to host the packages for creating and managing envs.
A rather complete discussions is on
conda-forge/pyproj-feedstock#17
conda-forge/basemap-feedstock#30

if you don't use a conda environment you have to set the env var and path yourself. Just search on the discussion about this

conda install -c conda-forge proj4
export PROJ_LIB=$CONDA_PREFIX/share/proj

@sarimurrab
Copy link

In the notebook BinderNotebook Executing

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

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'

This gonna work for sure, but just check where is your Anaconda Path up to share folder(WINDOWS)

import os
os.environ["PROJ_LIB"] = "C:\\Users\\SARIM\\Anaconda3\\Library\\share"; 
from mpl_toolkits.basemap import Basemap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants