KeyError: 'PROJ_LIB' when importing basemap in jupyter

Just started lesson Visualizing Geographic Data, I was following the install instructions for basemap on anaconda, but when I imported the module, this error pops up:

KeyError                                  Traceback (most recent call last)
<ipython-input-2-bae2d432abc4> in <module>
----> 1 from mpl_toolkits.basemap import basemap

~/opt/anaconda3/lib/python3.7/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={}

~/opt/anaconda3/lib/python3.7/os.py in __getitem__(self, key)
    677         except KeyError:
    678             # raise KeyError with the original key value
--> 679             raise KeyError(key) from None
    680         return self.decodevalue(value)
    681 

KeyError: 'PROJ_LIB'

I Googled the error and got straight to StackOverflow but the answers are beyond my understanding.

1 Like

Hi @NicoGuglielmo:

Could you try opening Anaconda prompt and typing these sequentially? After which you can rerun jupyter.
image
Sometimes packages refuse to install in the base environment so you can try creating, activating and thereafter installing the required packages and dependencies in the new environment. (Enter y for yes when prompted by the prompt window)

Remember to also launch the notebook using the correct environment (in this case testbm) using this toggle as shown below and then click Launch/Install using the notebook widget.

Hope this helps!

1 Like

Yeah, this was a painful one to work out. But I was working on Windows, so the solution I have will not work for you. Based on what you shared, it seems you are working on Linux.

You will have to put in the effort here to figure out the Stackoverflow answers.

This one is probably the easiest one for this - https://stackoverflow.com/a/54788001

import os
import conda

conda_file_dir = conda.__file__
conda_dir = conda_file_dir.split('lib')[0]
proj_lib = os.path.join(os.path.join(conda_dir, 'share'), 'proj')
os.environ["PROJ_LIB"] = proj_lib

from mpl_toolkits.basemap import Basemap

In your Notebook, just copy-paste the above and run the Cell. It should, ideally, work without trouble.

If the above doesn’t work, then try this other one - https://stackoverflow.com/a/58751730

import os
os.environ["PROJ_LIB"] = os.path.join(os.environ["CONDA_PREFIX"], "share", "proj")

Remove the other code you tried before, add this one above, and run the Cell. This one should work for you even if you are not on Linux. So, try it out as well.

If both of these don’t work then maybe another Linux user can help you out or you will have to learn to get comfortable with going through the Stackoverflow answers and trying some of those things out. Mine was on Windows, and I used a different approach than above, so I can’t help more than this.

Good luck!

1 Like

I´m a ranked amateur to be honest, so Im still not quite familiar with Anaconda Prompt and Terminals, I just tried opening a new terminal on Jupyter but right after the first line I got an error:
´´´
File “”, line 1
conda create -n testbm
^
´´´

This one worked fine! Thanks a lot for the answer! :smile:

1 Like

By the way, you should really explain a little further onto terminals, prompts and Anaconda in this course. I felt quite frustrated trying to solve this and the answers I got on StackOverflow and Github where really complex.
For a second there I thought how can I become a Data Scientist if I don´t even now how to use Anaconda prompt or install a module.
Just that, thanks a lot for the help!

1 Like

Hmm that could be some useful feedback @Sahil could take into consideration.

2 Likes

Hmm yeah @NicoGuglielmo, you should open the anaconda prompt (you can search it using the start menu on Windows) instead of the jupyter terminal or by launching the Ananconda navigator and going to envrionments > base(root) and selecting Open with Terminal using the drop down menu in future. It will be good if you could do some reading on how to use the anaconda prompt for data science.

2 Likes

Thanks a lot for the info! :slight_smile:

Thank you so much for the feedback. I will let the content team know about it.

Best,
Sahil

1 Like