MAC系统Python安装Basemap

重点:请保证Python3.6这个版本!!!

本方法仅限使用Anaconda安装!

  1. 安装Anaconda,访问《Anaconda下载页》

一、如果使用Terminal进行命令行控制

  1. 检查Anaconda是否已经安装完毕
conda -V
conda 4.9.1
  1. 使用Anaconda创建虚拟环境
conda create --name py36 python=3.6
  1. 激活环境py36
conda activate py36
  1. 安装Basemap
conda install basemap
  1. 轻松搞定
(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进行界面控制

  1. 打开Anaconda Navigator,切换到Environments

    切换到Environments

  2. 新建虚拟环境,命名为py36

    新建虚拟环境

  3. 搜索basemap,并Apply

    basemap

  4. 轻松搞定

(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)
 in 
      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 
     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

运行basemap示例

终于。
坑!

你可能感兴趣的:(MAC系统Python安装Basemap)