问题
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?
回答1:
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).
回答2:
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
回答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'
回答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
回答5:
This seems to be a common issue. There are several bug reports about it
- basemap/issues/419,
- https://github.com/matplotlib/basemap/issues/428
- basemap/issues/428
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.
回答6:
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.
回答7:
Proj4 easy solution to fix on pycharm is goto setting-> project interpreter -> + -> write proj4 in the search field and install.
回答8:
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).
来源:https://stackoverflow.com/questions/52295117/basemap-import-error-in-pycharm-keyerror-proj-lib