MAC系統(tǒng)Python安裝Basemap

重點:請保證Python3.6這個版本?。?!

本方法僅限使用Anaconda安裝!

  1. 安裝Anaconda,訪問《Anaconda下載頁》

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

  1. 檢查Anaconda是否已經(jīng)安裝完畢
conda -V
conda 4.9.1
  1. 使用Anaconda創(chuàng)建虛擬環(huán)境
conda create --name py36 python=3.6
  1. 激活環(huán)境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
>>>

以上,就算是完成了!
繼續(xù)看,后邊會有彩蛋?。?!

二、如果使用Anaconda Navigator進行界面控制

  1. 打開Anaconda Navigator,切換到Environments

    切換到Environments

  2. 新建虛擬環(huán)境,命名為py36

    新建虛擬環(huán)境

  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
>>>

重點來啦?。?!
你可能發(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示例

終于。
坑!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容