14

I'm trying to install and import the Basemap library into my Jupyter Notebook, but this returns the following error:

KeyError: 'PROJ_LIB'

After some research online, I understand I'm to install Basemap on a separate environment in Anaconda. After creating a new environment and installing Basemap (as well as all other relevant libraries), I have activated the environment. But when importing Basemap I still receive the same KeyError.

Here's what I did in my MacOS terminal:

conda create --name Py3.6 python=3.6 basemap
source activate Py3.6
conda upgrade proj4
env | grep -i proj
conda update --channel conda-forge proj4

Then in Jupyter Notebook I run the following:

from mpl_toolkits.basemap import Basemap

Can anyone tell me why this results in a KeyError?

RubenB
  • 141
  • 1
  • 1
  • 4

5 Answers5

15

Need to set the PROJ_LIB environment variable either before starting your notebook or in python with os.environ['PROJ_LIB'] = '<path_to_anaconda>/share/proj'

Ref. Basemap import error in PyCharm —— KeyError: 'PROJ_LIB'

RandyP
  • 497
  • 3
  • 11
  • 1
    Thank you so much. ---------------------------------------------------------------------------------------------------------------- import os;#-------- os.environ['PROJ_LIB'] = '/MYFOLDER/anaconda3/share/proj';#------- mpl_toolkits.basemap import Basemap – Edwin Torres Nov 15 '18 at 16:08
  • Is there really no other way than putting that line into the code?! I got this error after building a new conda environment from scratch. – Lukas Feb 22 '19 at 13:24
  • 1
    OK: I edited /home/.../miniconda2/envs/.../lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.py to include the line of your answer, so that the variable is set regardless from where the plotting script is executed. – Lukas Feb 22 '19 at 13:35
  • For SageMaker notebooks, I had to do this (painful): # conda install basemap # conda install -c conda-forge proj4 # conda install -c conda-forge basemap-data-hires # cp /home/ec2-user/anaconda3/pkgs/proj4-5.2.0-he6710b0_1/share/proj/epsg /home/ec2-user/anaconda3/share import os os.environ["PROJ_LIB"] = "/home/ec2-user/anaconda3/share" import matplotlib as mpl import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap – ski_squaw May 28 '19 at 22:37
  • I don't see the solution anywhere, but the following will be consistent for deploying notebooks anywhere. `os.environ['PROJ_LIB'] = os.environ['CONDA_PREFIX'] + '/share/proj'` – Geoffrey Garrett Jul 14 '20 at 09:39
12

In Windows 10 command line: first find the directory where the epsg file is stored:

where /r "c:\Users\username" epsg.*

...

c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share\epsg

...

then either in command line:

activate envname

SET PROJ_LIB=c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share

(make sure there are no leading on trailing spaces in the path!) and then

jupyter notebook

or add this to your jupyter notebook (as suggested by john ed):

import os

os.environ['PROJ_LIB'] = r'c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share'
Pavlo
  • 141
  • 1
  • 6
  • This solution does work. Thanks for your contribution, @Pavlo. The key was to set the `os.environ['PROJ_LIB']` to the path of the folder that contains the `epsg` file. – Nemo Dec 23 '19 at 12:05
  • I fixed it by coding in the Anaconda Jupyter notebook: import os os.environ['PROJ_LIB'] = r'c:\Users\Paisa\anaconda3\Library\share', after trying MANY other proposals – Hermes Morales Mar 25 '20 at 11:56
7

The problem occurs as the file location of "epsg" and PROJ_LIB has been changed for recent versions of python, but somehow they forgot to update the init.py for Basemap. If you have installed python using anaconda, this is a possible location for your espg file:

C:\Users\(xxxx)\AppData\Local\Continuum\anaconda3\pkgs\proj4-5.1.0-hfa6e2cd_1\Library\share

So you have to add this path at the start of your code in spyder or whatever field you are using.

import os

os.environ['PROJ_LIB'] = r'C:\Users\(xxxx)\AppData\Local\Continuum\anaconda3\pkgs\proj4-5.1.0-hfa6e2cd_1\Library\share'

from mpl_toolkits.basemap import Basemap
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
john ed
  • 331
  • 2
  • 7
1

If you can not locate epsg file at all, you can download it here:

https://raw.githubusercontent.com/matplotlib/basemap/master/lib/mpl_toolkits/basemap/data/epsg

And copy this file to your PATH, e.g. to:

os.environ['PROJ_LIB'] = 'C:\Users\username\Anaconda3\pkgs\basemap-1.2.0-py37h4e5d7af_0\Lib\site-packages\mpl_toolkits\basemap\data\'

This is the ONLY solution that worked for me on Windows 10 / Anaconda 3.

evanca
  • 509
  • 6
  • 9
-3

Launch Jupyter Notebook from command prompt and it won't throw the same error. It somehow works for me!

Tejas Kothari
  • 75
  • 1
  • 4