Basemap library using Anaconda Jupyter Notebooks - KeyError: PROJ_LIB
Asked Answered
B

5

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?

Brownell answered 21/10, 2018 at 0:40 Comment(0)
C
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'

Caretaker answered 23/10, 2018 at 17:29 Comment(5)
Thank you so much. ---------------------------------------------------------------------------------------------------------------- import os;#-------- os.environ['PROJ_LIB'] = '/MYFOLDER/anaconda3/share/proj';#------- mpl_toolkits.basemap import BasemapLyford
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.Dovetail
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.Dovetail
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 BasemapInbreeding
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'Teratoid
B
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'
Baer answered 8/1, 2019 at 7:56 Comment(2)
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.Sulph
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 proposalsPaulapauldron
A
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
Anana answered 16/11, 2018 at 23:9 Comment(0)
N
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.

Nielsen answered 16/3, 2020 at 10:47 Comment(0)
C
-3

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

Cop answered 27/12, 2018 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.