How can I avoid PROJ_LIB error in importing basemap?
Asked Answered
C

3

5

I tried to import basemap as follows in Python:

from mpl_toolkits.basemap import Basemap

and I get the following error:

Traceback (most recent call last):

  File "<ipython-input-16-880204a64918>", line 2, in <module>
    from mpl_toolkits.basemap import Basemap

  File "C:\Users\bakhadher\AppData\Local\Continuum\anaconda3\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 155, in <module>
    pyproj_datadir = os.environ['PROJ_LIB']

  File "C:\Users\bakhadher\AppData\Local\Continuum\anaconda3\lib\os.py", line 678, in __getitem__
    raise KeyError(key) from None

KeyError: 'PROJ_LIB'
Crabbe answered 15/1, 2019 at 15:29 Comment(3)
Useful link here: github.com/matplotlib/basemap/issues/419Jac
This is caused by conda installation when under the base environment. So you either a) set PROJ_LIB in your environment (~/.bashrc) or b) kludge PROJ_LIB inside your Python code, before importing from basemap or c) reinstall basemap under an activated conda environmentCephalonia
"This is caused by conda installation when under the base environment", you're mixing causes and consequences. This is caused by developers assuming everyone should use conda in a certain way and renounce to other. The consequence is the install fails.Intake
F
7

From here: https://github.com/conda-forge/basemap-feedstock/issues/30#issuecomment-423512069

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```
Frail answered 31/1, 2019 at 14:2 Comment(3)
I tried the approach and it gives error "FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Anaconda_Python\\share\\proj\\epsg'" And in fact I do not have folder 'proj' in this localisation, even thou I installed proj packageCastaneda
This worked for me with anaconda3 on both Linux and OSX. Not sure why, but on OSX, the problem was only occurring when the bash script which invoked the python program was called by cron; calling the bash script from the command line worked without the same problem.Huygens
@AAAA: "I tried the approach and it gives error "FileNotFoundError then search for file EPSG (no extension). It will be in a directory/folder like <path>/share/basemap/ the environment variable PROJ_LIB is to set to value <path>/share (i.e. ignoring the last directory/folder). The code in this answer is just setting the variable, so you can do it manually.Intake
A
1
  1. Reintall package(version problem could cause it)
conda install -c conda-forge proj4
conda install basemap
  1. Set enviroment: setenv PROJ_LIB "$CONDA_PREFIX/share/proj"
  2. in python script(need to be custom in different environment)
import os
os.environ['PROJ_LIB'] = '/glade/u/home/lixujin/work/anaconda3/envs/AC_tools/share/proj'
Aspergillum answered 3/10, 2019 at 17:59 Comment(1)
Where should I write "$CONDA_PREFIX/share/proj" in Anaconda prompt or in Spyder? Both give errors. If I co to "D:\Anaconda_Python\share" there is no folder "proj"Castaneda
B
0

I have conda, usually I have this mistake and my solution was:

import os
os.environ['PROJ_LIB'] = '/home/YOUR_USER/anaconda3/share/proj'
from mpl_toolkits.basemap import Basemap
import cartopy.crs as crs
Brunabrunch answered 8/1, 2020 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.