重點:請保證
Python為3.6這個版本?。?!
本方法僅限使用Anaconda安裝!
- 安裝
Anaconda,訪問《Anaconda下載頁》
一、如果使用Terminal進行命令行控制
- 檢查
Anaconda是否已經(jīng)安裝完畢
conda -V
conda 4.9.1
- 使用
Anaconda創(chuàng)建虛擬環(huán)境
conda create --name py36 python=3.6
- 激活環(huán)境
py36
conda activate py36
- 安裝
Basemap
conda install basemap
- 輕松搞定
(py36) ? ~ python
Python 3.6.12 |Anaconda, Inc.| (default, Sep 8 2020, 17:50:39)
[GCC Clang 10.0.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mpl_toolkits.basemap import Basemap
>>>
以上,就算是完成了!
繼續(xù)看,后邊會有彩蛋?。?!
二、如果使用Anaconda Navigator進行界面控制
-
打開
Anaconda Navigator,切換到Environments
切換到Environments -
新建虛擬環(huán)境,命名為
py36
新建虛擬環(huán)境 -
搜索
basemap,并Apply
basemap 輕松搞定
(py36) ? ~ python
Python 3.6.12 |Anaconda, Inc.| (default, Sep 8 2020, 17:50:39)
[GCC Clang 10.0.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mpl_toolkits.basemap import Basemap
>>>
重點來啦?。?!
你可能發(fā)現(xiàn)你引用成功了,但是代碼報錯
import numpy as np...
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-f0f1258261f7> in <module>
1 import numpy as np
2 import matplotlib.pyplot as plt
----> 3 from mpl_toolkits.basemap import Basemap
4
5 plt.figure(figsize=(8, 8))
~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py in <module>
24
25 from matplotlib import __version__ as _matplotlib_version
---> 26 from matplotlib.cbook import dedent
27 # check to make sure matplotlib is not too old.
28 _matplotlib_version = LooseVersion(_matplotlib_version)
ImportError: cannot import name 'dedent'
然后,我們?nèi)?code>matplotlib下的cbook里搜一下,發(fā)現(xiàn)有引用但是沒有定義dedent...
補上dedent的方法
def dedent(s):
"""
Remove excess indentation from docstring *s*.
Discards any leading blank lines, then removes up to n whitespace
characters from each line, where n is the number of leading
whitespace characters in the first line. It differs from
textwrap.dedent in its deletion of leading blank lines and its use
of the first non-blank line to determine the indentation.
It is also faster in most cases.
"""
# This implementation has a somewhat obtuse use of regular
# expressions. However, this function accounted for almost 30% of
# matplotlib startup time, so it is worthy of optimization at all
# costs.
if not s: # includes case of s is None
return ''
match = _find_dedent_regex.match(s)
if match is None:
return s
# This is the number of spaces to remove from the left-hand side.
nshift = match.end(1) - match.start(1)
if nshift == 0:
return s
# Get a regex that will remove *up to* nshift spaces from the
# beginning of each line. If it isn't in the cache, generate it.
unindent = _dedent_regex.get(nshift, None)
if unindent is None:
unindent = re.compile("\n\r? {0,%d}" % nshift)
_dedent_regex[nshift] = unindent
result = unindent.sub("\n", s).strip()
return result
再試一次!記得重啟Kernel

運行basemap示例
終于。
坑!


