Basemap import error in PyCharm — KeyError: 'PROJ_LIB'

30,262

Solution 1

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

Solution 2

Following mewahl's comment I've added to my .bashrc (I use bash):

export PROJ_LIB=/path/to/your/instalation/of/anaconda/share/proj/

and now basemap (and others work).

Solution 3

You have to set the path of Proj lib as in newer version , this path has been replaced. Write below two lines of code before importing matplot_toolkits

  ### For Window's Users
      import os
      os.environ['PROJ_LIB'] = r'C:\Users\XXXXX\Anaconda3\pkgs\proj4-5.2.0- 
      ha925a31_1\Library\share'

To find the path of Proj_lib , just search epsg and then copy this epsg file location and put in proj_lib . Your Problem will be solved.

  ### For Linux's Users
  import os
  os.environ['PROJ_LIB'] = r'/home/XXXXXX/anaconda3/pkgs/proj4-5.2.0- 
  he6710b0_1/share/proj'

Solution 4

The answer is from Github and it worked for me.

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

Solution 5

This worked for me:

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

This extends @Yusuf Baktir 's answer by omitting hard-coding the path to the epsg file. This way the code works on any machine that has conda installed (and activated of course).

Share:
30,262
Yupei
Author by

Yupei

Updated on July 26, 2020

Comments

  • Yupei
    Yupei almost 4 years

    I tried to use Basemap package to plot a map by PyCharm, but I got something wrong with

    from mpl_toolkits.basemap import Basemap`
    

    And the Traceback as followed:

    Traceback (most recent call last):
    File "/Users/yupeipei/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-4-0a24a3a77efd>", line 7, in <module>
        from mpl_toolkits.basemap import Basemap
      File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 20, in do_import
        module = self._system_import(name, *args, **kwargs)
      File "/Users/yupeipei/anaconda3/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py", line 146, in <module>
        pyproj_datadir = os.environ['PROJ_LIB']
      File "/Users/yupeipei/anaconda3/lib/python3.6/os.py", line 669, in __ getitem__
        raise KeyError(key) from None
    KeyError: 'PROJ_LIB'
    

    I'm confused with this error on PyCharm, because the same script is running correctly on Jupyter or Spyder! The environment in PyCharm is ../anaconda3/lib/python3.6 where is same from anaconda.

    Has anyone met this error before?

    Could anyone can help me to solve this error?