Basemap import error in PyCharm — KeyError: 'PROJ_LIB'
Asked Answered
T

10

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?

Titoism answered 12/9, 2018 at 12:24 Comment(2)
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.)Hush
I had this problem after installing Anaconda 5.7.Lotti
R
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
Risa answered 12/12, 2018 at 21:53 Comment(0)
S
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).

Sicanian answered 13/9, 2018 at 13:47 Comment(4)
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.Sicanian
Here a good way to solve this problem [#52911732Dygall
Great answer, this saved my day.Smaragd
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.Assort
V
15

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'
Voelker answered 16/5, 2019 at 10:58 Comment(2)
I was looking for this for months! finally it worksApothem
@Voelker Thank you very much for this solution. I have been working on this for the whole week. and this solution helped me resolve the issue. Thanks again.Sollie
E
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
Exposition answered 20/2, 2019 at 13:53 Comment(0)
C
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).

Cavin answered 7/11, 2019 at 15:12 Comment(0)
M
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.

Mcvay answered 19/11, 2018 at 11:44 Comment(0)
F
0

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

Firry answered 23/11, 2018 at 0:9 Comment(0)
C
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

Crude answered 25/7, 2020 at 17:22 Comment(0)
W
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.

Whatever answered 24/3, 2023 at 9:28 Comment(0)
F
-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.

Firry answered 12/9, 2018 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.