重点:请保证
Python
为3.6
这个版本!!!
本方法仅限使用Anaconda
安装!
- 安装
Anaconda
,访问《Anaconda下载页》
一、如果使用Terminal
进行命令行控制
- 检查
Anaconda
是否已经安装完毕
conda -V
conda 4.9.1
- 使用
Anaconda
创建虚拟环境
conda create --name py36 python=3.6
- 激活环境
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
>>>
以上,就算是完成了!
继续看,后边会有彩蛋!!!
二、如果使用Anaconda Navigator
进行界面控制
-
打开
Anaconda Navigator
,切换到Environments
切换到Environments
-
新建虚拟环境,命名为
py36
新建虚拟环境
-
搜索
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
>>>
重点来啦!!!
你可能发现你引用成功了,但是代码报错
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'
然后,我们去matplotlib
下的cbook
里搜一下,发现有引用但是没有定义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
![](https://img.haomeiwen.com/i962286/014b69d481a492df.png)
终于。
坑!
网友评论