Can't install Basemap

I am currently using Python 3.7 through Anaconda and I keep getting an error while trying to install Basemap in Jupyter Notebook.

from mpl_toolkits.basemap import Basemap

This is the error:
KeyError: ‘PROJ_LIB’

I have done research on fixing the problem through Stackoverflow, and other sources, but I still haven’t found a solution. Please help.

Try this solution:

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

For more info, check here.

Thank you for responding. I tried your suggestion…and actually I had tried something similar after reading someone’s post.

Still getting the following error message when I enter the last line of code:

FileNotFoundError: [Errno 2] No such file or directory: ‘C:\ProgramData\Anaconda3\share\proj\epsg’

HI, I faced the same issues last week and experienced several of failed solutions on the internet. But this post works to me like a charm:

For Windows 10 with Anaconda + Python 3.71 (and I’m sure other Python 3 versions and Windows 7/8), you can tell Basemap where Proj4’s “epsg” file is to succeed. I don’t have an “environment” or whatever because it’s too much work to figure out - so I didn’t have an anaconda\share\proj area (as far as I could discern why I didn’t have it).

But, what Basemap wants is the file “epsg”, search the Anaconda directory for it with Windows Explorer. If it doesn’t find it, install Proj4 by opening the “Anaconda Prompt” and typing in:

conda install -c conda-forge proj4

If it finds it, it should be in something like:

C:\Utilities\Python\Anaconda\Library\Share (it’s where mine was, as well as \pkgs\ places where I guess it puts the package itself - and those can work too if need be, I used them at first, but the library one should work through updates better (maybe)).

Use the following code before importing Basemap and it’ll work. Sets the environment variable PROJ_LIB to wherever epsg is and then Basemap can be happy.

import os
os.environ["PROJ_LIB"] = "C:\\Utilities\\Python\\Anaconda\\Library\\share"; #fixr
from mpl_toolkits.basemap import Basemap

As a nice bonus, to get hi-res data of Basemap, which Anaconda doesn’t include in the Basemap install to start, type into “Anaconda Prompt”:

conda install -c conda-forge basemap-data-hires

Ok thx for your help. I’ll try it. In the meantime I just tried to use another plotting package to move ahead in the project - Geopandas.

So it definitely worked! Thank you so much for your help! Yeah I didn’t understand the whole “environment” thing either.

1 Like

You’re welcome ! I am switching to Data Analyst role from Accountant so I learn to use tools without deep knowledge to programming languages and its environments. Just search Internet when you stuck on somewhere.

1 Like

Your post worked for me. Even though I have no clue what your post made me do, it was a better solution than anything I could find online. Thanks a lot!

Awesome! Happy to hear that it worked!

It worked for me too. Just being specific, this is the version I’m running.

conda version : 4.8.3
conda-build version : 3.10.5
python version :

Thank you.

Dont know how but it worked. SUPER APPRECIATION!

I searched for it on the internet, including stack overflow, from where this is taken from… but could only find it here after searching for it for many days.
Thanks much for posting it here.
I wish Dataquest included the link to this community discussion on that exact mission page @Mary do we need a ticket to bring this to the notice of course makers

Thank you very much @danhthenguyen_nt, your solution worked perfectly also for me!