Basemap library using Anaconda Jupyter Notebooks - KeyError: PROJ_LIB

喜欢而已 提交于 2019-11-29 03:55:45

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'

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'

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!