我在将Python 3.2.7升级到3.3.6时,遇到了导入Basemap的相同错误。
错误消息源于您尝试从mpl_toolkits.basemap中导入Basemap,但是mpl_toolkits.basemap模块需要从matplotlib.cbook模块导入dedent函数,但该函数不在那里。
因此,我想有两个可能的解决方案:注释导入该函数的行或复制它。 我选择了第二个选项。
我不知道为什么matplotlib.cbook模块中没有dedent函数。
这是我在链接上找到的dedent函数,它还包括标题:@deprecated(“3.1”,alternative =“inspect.cleandoc”)
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.
"""
if not s:
return ''
match = _find_dedent_regex.match(s)
if match is None:
return s
nshift = match.end(1) - match.start(1)
if nshift == 0:
return s
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
我从matplotlib网站上的
https://matplotlib.org/3.1.1/_modules/matplotlib/cbook.html#dedent复制了函数'dedent',并将其放入了 matplolib\cbook 的
init.py 模块内。现在它能够正常工作。
请注意,要将其复制到正确的行,因为模块中有些变量是预定义好的,例如以下行中的 _find_dedent_regex:
match = _find_dedent_regex.match(s)
和_dedent_regex用于行:
unindent = _dedent_regex.get(nshift, None)
if unindent is None:
unindent = re.compile("\n\r? {0,%d}" % nshift)
_dedent_regex[nshift] = unindent
这是我在模块中放置函数的位置
对于我可能犯的拼写和/或语法错误,我深表歉意,我将尽力纠正那些我可能错过并向我报告的错误。
希望这对您有所帮助。
matplotlib
库吗? - Karthik