33

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?

sniperd
  • 5,124
  • 6
  • 28
  • 44
Yupei
  • 411
  • 1
  • 5
  • 9
  • I began experiencing this error about a week ago. I think it's caused by a recent update to the Anaconda pyproj package. I found that I could edit line 146 of `.../site-packages/mpl_toolkits/basemap/__init__.py` to point directly to where the PROJ library was installed, instead of looking for an environment variable. (In my case, the anaconda pyproj package installed the PROJ library at `/anaconda/envs/[my env name]/share/proj` -- YMMV.) – mewahl Sep 12 '18 at 16:31
  • I had this problem after installing Anaconda 5.7. – blaylockbk Oct 17 '18 at 20:48

10 Answers10

27

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
user2403531
  • 688
  • 6
  • 15
20

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

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user1770719
  • 301
  • 1
  • 4
  • 1
    It solved my problem.You can consider it a workaround, but the solutions pointed in the link in my comment did NOT solve the issue. – user1770719 Sep 14 '18 at 12:16
  • 3
    Here a good way to solve this problem [https://stackoverflow.com/questions/52911232/basemap-library-using-anaconda-jupyter-notebooks-keyerror-proj-lib?noredirect=1&lq=1] – Edwin Torres Nov 15 '18 at 16:14
  • Great answer, this saved my day. – Jinhua Wang Nov 22 '18 at 16:54
  • In addition to setting the PROJ_LIB var, I had to downgrade pyproj to 1.9.6 for it to work, otherwise the epsg file was missing. – Itamar Katz Dec 23 '19 at 14:54
14

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
1__
  • 1,511
  • 2
  • 9
  • 21
14

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'
sameer_nubia
  • 721
  • 8
  • 8
13

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

Marjan Moderc
  • 2,747
  • 23
  • 44
2

This seems to be a common issue. There are several bug reports about it

I had run into this error myself, and for me the solution was to uninstall basemap 1.2, then install basemap 1.1 from an old wheel file I still had lying around and then install basemap 1.2 again. I honestly have no idea why that worked though.

Also from those issues above there are all kinds of other solutions that people have reported to work for them. Hopefully one of them fits here as well.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

Proj4 easy solution to fix on pycharm is goto setting-> project interpreter -> + -> write proj4 in the search field and install.

0

I was not able to solve this problem but I was able to find an alternate, use CartoPy. basemap is being maintained for python 2.7 users. CartoPy is a better alternative

Adarsh_V_Desai
  • 195
  • 1
  • 8
0

I believe this error occurs when having mixed versions of the basemap, basemap-data and basemap-data-hires packages. For me following the instructions on basemap-data-hires not found despite being installed solved it.

-2

I faced same problem. I installed anaconda and install conda install -c anaconda basemap.

I used Anaconda built in IDE named "Spyder". Spyder is better than pycharm. only problem with spyder is lack of intellisense.

I solved problem of Proj4 by setting path.

Other problem kernel restarting when loading of .json larger file dataset.

I use notepad++ and 010 editor to re-save file in small chunks and at last I merged all outputs.