As an Anaconda user, I love how convenient it is to install packages. So when I tried install Basemap using conda as usual:
conda install -c anaconda basemap
And import it in Jupyter Notebook:
from mpl_toolkits.basemap import Basemap
I was shocked that I got an error message:
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-2-702dba317ad4> in <module>() 1 ----> 2 from mpl_toolkits.basemap import Basemap ~\Anaconda3\lib\site-packages\mpl_toolkits\basemap\__init__.py in <module>() 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\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'
I googled for solution, and it seems to be a problem with conda not Basemap. Some people suggested installing using pip, but I still got error message when installing with pip.
And then I figured out that the path “PROJ_LIB” is needed for the file epsg. So where is the file? I looked at the Basemap documentation and it said:
The epsg codes supported by Basemap are at the file /mpl_toolkits/basemap/data/epsg.
Yet, I couldn’t even find the “data” folder under the basemap directory.
Luckily, I got a MacBook near me, which has Basemap installed (and it worked). So I looked at the “PROJ_LIB” variable, and copy the epsg file in the folder and paste to the basemap folder on my Windows computer.
Update: As mentioned in the comments, the epsg file can also be found on GitHub in the basemap repository!
Before importing Basemap, I added this two lines.
import os
os.environ['PROJ_LIB'] = 'C:/Users/USERNAME/Anaconda3/Lib/site-packages/mpl_toolkits/basemap'
And it worked.
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='cyl', resolution='l',
llcrnrlat=-90, urcrnrlat = 90,
llcrnrlon=-180, urcrnrlon = 180)
m.drawcoastlines(linewidth=0.5)
This is, of course, not the best solution of the problem, because you have to define the “PROJ_LIB” everytime you use Basemap. Still, I think it is quick enough if you don’t want to bother with pip.
Wow, thank you so much! Would have been a dead end to a newbie like me. I was able to find the epsg file on github at:
https://github.com/matplotlib/basemap/blob/master/lib/mpl_toolkits/basemap/data/epsg
and like you said, put it in the basemap directory, do not add a data folder.
LikeLiked by 2 people
Thanks for the comment! Glad to know that it helps!
LikeLike
Please how do i get the epsg file to my machine?
LikeLike
As said in the previous comment, the epsg file can be found here: https://github.com/matplotlib/basemap/blob/master/lib/mpl_toolkits/basemap/data/epsg
Hope it helps. I have updated the blog post with the link.
LikeLike
Anna, you saved me!!! Thank you so much. I also googled and tried several other approaches, but all failed. Now, I just followed your instruction and it works!!!
LikeLiked by 1 person
Anna,
I followed all of your instruction, I added the two-line all the to the bottom of the file I found on Github, I saved it and then imported base map, it gives me this error: Please help. Thanks!
File “”, line 2
m = Basemap(projection=’cyl’, resolution=’l’,
^
IndentationError: unexpected indent
LikeLike
It seems that the error occurred in the main program. Can you check if there are any (hidden) space or tab before the “m”?
LikeLiked by 1 person
Even after following your instructions i am getting same error can you please help me
LikeLike
Can you show me your code and the error messages? (maybe a screenshot)
It is important to make sure the directory with the epsg file is the same as defined in os.environ[‘PROJ_LIB’].
LikeLike
Hello
Can you explain a little more how you did the following=
Luckily, I got a MacBook near me, which has Basemap installed (and it worked). So I looked at the “PROJ_LIB” variable, and copy the epsg file in the folder and paste to the basemap folder on my Windows computer.
LikeLike
Hello,
It’s because I couldn’t find the epsg file in the folder specified at the PROJ_LIB, so I tried to find it elsewhere. I didn’t realize that I can also find the file on the basemap GitHub, which should be accessible for the public. Hope this can help.
LikeLike
Hi, I feel like I’ve followed the steps correctly but its still not working for me. Can i send you a screenshot of what I’m seeing?
LikeLike
Yes! You can send me the screenshot / error messages so we can figure out what’s wrong.
LikeLike
Hi Anna,
Did you save epsg as a text document? Additionally, I think my paths are not matching up even though I have double checked the location of epsg. Could I send you a screenshot?
LikeLike
Hi, I didn’t save the epsg as a text document – it shouldn’t have any extensions. If the problem is not solved, you can send me a screenshot to see what’s going on. If you don’t care, you may post the screenshot / error messages here.
LikeLike
Hey Anna thank you for sharing this.It helped me a lot.Though i had to change the first line a little to make it work for me as-
import os
os.environ[‘PROJ_LIB’] = r’C:/Users/USERNAME/Anaconda3/Lib/site-packages/mpl_toolkits/basemap’
(Had to write r before the path).
by the way i am a student of atmospheric sc and was actually using the basemap for project related work.So thank you.
LikeLike
WoW! I am also in atmospheric science too.
Now I would prefer Cartopy over Basemap because Basemap is no longer maintained, unfortunately
LikeLike
Hi,
Thank you so much… i got stuck with this error…
i just followed your instructions……
LikeLike
Hello!
I followed all the steps but unfortunately I still have problems. The new error is:
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\HP\Anaconda2\lib\site-packages\mpl_toolkits\basemap\__init__.py”, line 156, in
epsgf = open(os.path.join(pyproj_datadir,’epsg’))
IOError: [Errno 2] No such file or directory: ‘C:/Users/HP/Anaconda2/Lib/site-packages/mpl_toolkits/basemap\\epsg’
Do you know what is my problem?
LikeLike
Hi, may I know if you can access the espg file in the folder you specified here:
C:/Users/HP/Anaconda2/Lib/site-packages/mpl_toolkits/basemap\\epsg
LikeLike
Hi, so I am able to access the file epsg there but my path is different since Im on a mac. What else can I do? this is what i see when i drag the epsg over to my terminal.
/anaconda3/share/proj/epsg
LikeLike
thanks you very much,, its help me to finish my exam… hope you get lot of joy
LikeLike
Hey everybody,
the whole thing about importing basemap and the missing epgs-file is quite easier to handle:
Use your shell-prompt and type:
conda install -c conda-forge proj
; confirm with ‘yes’. If you want, type „conda install -c conda-forge proj-data“ afterwards (including the confirmation).
Have a look at https://proj.org/install.html.
And in case it did not work well and you are using ANACONDA, then use the ENVIRONMENTS-MANAGER to install the missing packages.
Best and good luck, Stefan
LikeLike
Thanks for your tip, Anna. I was a little nervous thinking of trying to apply this Windows solution to the Mac and Linux computers I’m working on, so I tried another solution I found at https://stackoverflow.com/questions/54201946/how-can-i-avoid-proj-lib-error-in-importing-basemap
It worked for me on both Mac and Linux; it might work for Windows, too.
LikeLiked by 1 person
Thank you so much!
LikeLike