[Solved] “PROJ_LIB” error when installing Basemap on Windows using Anaconda

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.

25 thoughts on “[Solved] “PROJ_LIB” error when installing Basemap on Windows using Anaconda

  1. 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!!!

    Liked by 1 person

  2. 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

    Like

    1. 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’].

      Like

  3. 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.

    Like

    1. 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.

      Like

  4. 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?

    Like

  5. 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?

    Like

    1. 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.

      Like

  6. 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.

    Like

    1. WoW! I am also in atmospheric science too.
      Now I would prefer Cartopy over Basemap because Basemap is no longer maintained, unfortunately

      Like

  7. 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?

    Like

    1. 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

      Like

      1. 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

        Like

  8. 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

    Like

Leave a comment