PyCharm 中的底图导入错误 — KeyError: 'PROJ_LIB' [英] Basemap import error in PyCharm — KeyError: 'PROJ_LIB'

查看:34
本文介绍了PyCharm 中的底图导入错误 — KeyError: 'PROJ_LIB'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Basemap 包通过 PyCharm 绘制地图,但我遇到了问题

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

from mpl_toolkits.basemap import Basemap`

回溯如下:

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'

我对 PyCharm 上的这个错误感到困惑,因为相同的脚本在 Jupyter 或 Spyder 上运行正确!PyCharm 中的环境是 ../anaconda3/lib/python3.6,与 anaconda 相同.

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?

推荐答案

对于带有 Anaconda + Python 3.71 的 Windows 10(我相信其他 Python 3 版本和 Windows 7/8),您可以告诉 Basemap Proj4 的epsg"在哪里"文件是成功的.我没有环境"或其他任何东西,因为要弄清楚的工作量太大 - 所以我没有 anacondashareproj 区域(据我所知,为什么我没有它).

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 anacondashareproj area (as far as I could discern why I didn't have it).

但是,Basemap 需要的是文件epsg",请使用 Windows 资源管理器在 Anaconda 目录中搜索它.如果找不到,请打开Anaconda Prompt"并输入以下内容来安装 Proj4:

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:UtilitiesPythonAnacondaLibraryShare (这是我的位置,以及我猜它放置包本身的 pkgs 地方 - 如果这些也可以工作需要,我一开始使用它们,但库应该更好地通过更新(也许).

C:UtilitiesPythonAnacondaLibraryShare (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)).

在导入底图之前使用以下代码,它会起作用.将环境变量 PROJ_LIB 设置为 epsg 所在的任何位置,然后 Basemap 即可.

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

作为一个不错的奖励,要获取 Basemap 的高分辨率数据(Anaconda 不包含在 Basemap 安装开始),请输入Anaconda Prompt":

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

这篇关于PyCharm 中的底图导入错误 — KeyError: 'PROJ_LIB'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆